Lesson 14 : PHP Script for Login & Louout.

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.
3When 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 

$_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">
<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"/>
if(isset($_SESSION['user']) && $_SESSION['user']!="")
header("Location: login.php");
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset style="width:300px;">
<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" />

Example page login.php

header("Location: index.php");
echo "Congratulation you are logged in!!";
<a href="logout.php" >Logout</a>

Example page logout.php

header("Location: index.php");

Previous Post Next Post