Login-Logout is a very basic mechanism for authenticate a user User of your website or application. this mechanism always implemented using PHP session variable.
$_SESSION is an associative array containing session variables available to the current script.
for registering a session variable we must have to start_session.
About session_start():-
Syntax : -
bool session_start ( void )
Returns TRUE if a session successfully started otherwise FALSE.
1. session_start — Initialize session data.
2. session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
3. When session.use_trans_sid is enabled, the session_start() function will register an internal output handler for URL rewriting.
PHP script for login logout :-
Example page index.php
<?php
session_start();
if(isset($_POST['submit']))
{
$_SESSION['user'] = "user";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>log in page.</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<?php
if(isset($_SESSION['user']) && $_SESSION['user']!="")
{
header("Location: login.php");
}
?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<center>
<div>
<fieldset style="width:300px;">
<legend>Login</legend>
<label id="userid"><input type="text" id="userid" name="userid" /></label>
<label id="passwd"><input type="text" id="passwd" name="passwd" /></label>
<input type="submit" name="submit" value="go" />
</fieldset>
</div>
</center>
</form>
</body>
</html>
Example page login.php
<?php
session_start();
if(!isset($_SESSION['user']))
header("Location: index.php");
else
echo "Congratulation you are logged in!!";
?>
<a href="logout.php" >Logout</a>
Example page logout.php
<?php
session_start();
unset($_SESSION['user']);
header("Location: index.php");
exit;
?>
PHP SESSION variables and PHP login loout script
ReplyDeletegreat
ReplyDelete