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

Etiquetas:

ejemplos (200) if (79) extra (48) java (48) php (46) c++ (39) Python (31) descargas (31) javascript (28) vb.net (28) pascal (27) while (25) html (24) for (22) ejersicios (19) juegos (17) matriz (14) as3 (13) determinante (13) graficas (13) variables (12) tutoriales (11) url (10) Hola mundo (8) comentarios (8) Cursos (7) android (7) random (7) try (7) BaseDeDatos (6) array (4) asp (2) compone (1) poraquivas (1)

Google+ Followers

Visitas:

Flag Counter