login.php
<form action="" method="post" name="Login_Form">
<table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="left" valign="top"><h3>Login</h3></td>
</tr>
<tr>
<td align="right" valign="top">Username</td>
<td><input name="Username" type="text" class="Input"></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="Password" type="password" class="Input"></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login" class="Button3"></td>
</tr>
</table>
</form>
______________________________________________________
Step 2: Next we need to write a php script to check the
login authentication.
<?php session_start(); /* Starts the session */
/* Check Login form submitted */
if(isset($_POST['Submit'])){
/* Define username and associated password array */
$logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');
/* Check and assign submitted Username and Password to new variable */
$Username = isset($_POST['Username']) ? $_POST['Username'] : '';
$Password = isset($_POST['Password']) ? $_POST['Password'] : '';
/* Check Username and Password existence in defined array */
if (isset($logins[$Username]) && $logins[$Username] == $Password){
/* Success: Set session variables and redirect to Protected page */
$_SESSION['UserData']['Username']=$logins[$Username];
header("location:index.php");
exit;
} else {
/*Unsuccessful attempt: Set error message */
$msg="<span style='color:red'>Invalid Login Details</span>";
}
}
?>
____________________________________________________________
Step 3: If the login is correct then we need to redirect page to
protected area.So need an protected script page too.
index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
__________________________________________________________
index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
Congratulation! You have logged into password protected page.
<a href="logout.php">Click here</a> to Logout.
__________________________________________________________
logout.php
<?php session_start(); /* Starts the session */
session_destroy(); /* Destroy started session */
header("location:login.php"); /* Redirect to login page */
exit;
?>
Congratulation! You have logged into password protected page. <a href="logout.php">Click here</a> to Logout.
Ready, now our login system perfectly done but we also need to provide a logout facility to user, so we require to create a logout page.
logout.php
Example:
<?php session_start(); /* Starts the session */
session_destroy(); /* Destroy started session */
header("location:login.php"); /* Redirect to login page */
exit;
?>
<form action="" method="post" name="Login_Form">
<table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="left" valign="top"><h3>Login</h3></td>
</tr>
<tr>
<td align="right" valign="top">Username</td>
<td><input name="Username" type="text" class="Input"></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="Password" type="password" class="Input"></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login" class="Button3"></td>
</tr>
</table>
</form>
______________________________________________________
Step 2: Next we need to write a php script to check the
login authentication.
<?php session_start(); /* Starts the session */
/* Check Login form submitted */
if(isset($_POST['Submit'])){
/* Define username and associated password array */
$logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');
/* Check and assign submitted Username and Password to new variable */
$Username = isset($_POST['Username']) ? $_POST['Username'] : '';
$Password = isset($_POST['Password']) ? $_POST['Password'] : '';
/* Check Username and Password existence in defined array */
if (isset($logins[$Username]) && $logins[$Username] == $Password){
/* Success: Set session variables and redirect to Protected page */
$_SESSION['UserData']['Username']=$logins[$Username];
header("location:index.php");
exit;
} else {
/*Unsuccessful attempt: Set error message */
$msg="<span style='color:red'>Invalid Login Details</span>";
}
}
?>
____________________________________________________________
Step 3: If the login is correct then we need to redirect page to
protected area.So need an protected script page too.
index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
__________________________________________________________
index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
Congratulation! You have logged into password protected page.
<a href="logout.php">Click here</a> to Logout.
__________________________________________________________
logout.php
<?php session_start(); /* Starts the session */
session_destroy(); /* Destroy started session */
header("location:login.php"); /* Redirect to login page */
exit;
?>
Congratulation! You have logged into password protected page. <a href="logout.php">Click here</a> to Logout.
Ready, now our login system perfectly done but we also need to provide a logout facility to user, so we require to create a logout page.
logout.php
Example:
<?php session_start(); /* Starts the session */
session_destroy(); /* Destroy started session */
header("location:login.php"); /* Redirect to login page */
exit;
?>
No comments:
Post a Comment