loggin logout php / ejemplo

Before attempting this, please ensure that you have installed an Apache/MySQL/PHP Stack,
you may use one of these program :
  • XAMPP (For many platform)
  • WAMP (For Windows)
  • MAMP (for Mac user)
Start the server by executing “Start Wamp Server” (for WAMP user, or similar for the other software). Then, you can view your page in any browser in your own computer by typing:
http://localhost/
if you can see your page there, Good job !!! :D :D , it means that your server has already running in your computer.




then let’s get into the php & html code.
first we create the simple login form like this : (in this example I create the login form with the name: niceform.php)
<?php
session_start();
if(isset($_SESSION['usr']) && isset($_SESSION['pswd'])){
header(“Location: content.php”);
}
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title> PHP Login </title>
</head>
<body>
<center>
<form method=”post” action=”login.php”>
<!– in this example I link it with login.php to check the password & username–>
<table>
<tr><td>Username:</td><td><input type=”text” name=”usr”></td></tr>
<tr><td>Password:</td><td><input type=”password” name=”pswd”></td></tr>
<tr><td><input type=”submit” name=”login” value=”Login”></td>
<td><input type=”reset” name=”reset” value=”Reset”></td></tr>
</table>
</form>
</center>
</body>
</html>


After finishing the form, let’s move to login.php, here we’ll check the variable of username and password,  if it’s correct, then it’ll be redirected to content.php , otherwise it’ll forced back into the niceform.php
login.php:

<?php

session_start();

if($_REQUEST['usr']==”ABC” && $_REQUEST['pswd']==”123″){

$_SESSION['usr'] = “ABC”;

$_SESSION['pswd'] = “123″;

header(“Location: content.php”);
}
else{
header(“Location: niceform.php”);
}
?>
then, we move to the content page content.php :

<?php

session_start();

if(!isset($_SESSION['usr']) || !isset($_SESSION['pswd'])){

header(“Location: niceform.php”);

}

include ‘logoff.html’;
<!– in this example my content only containing a html with logout link–>
?>
For logout function, I simply use session_destroy (for a simple log out function in this case) logout.php :

<?php

session_start();

session_destroy();

header(‘Location: niceform.php’);

exit;

?>
finally, create a html file that containing a link for logout : logoff.html :

<html>

<head>

<title></title>

</head>

<body>

<center>
<h2><a href=”./logout.php”>Logout</a></h2>
<br/>
</center>
</body>
</html>
Btw make sure that your pages are correctly stored under the appropriate server directory i.e
\wamp\www —> for WAMP user
before you start typing them, make sure that you read them well, study them and see the relation between each page.
I hope these codes can enlighten your understanding about php session.


Original; loggin-logout

Comentarios

Entradas más populares de este blog

Ejemplo de suma Pascal

Solucion al error collation desconocida: 'utf8mb4_0900_ai_ci'