Sessions allow the PHP script to store data on the web server that can be later used, even between requests to different PHP pages. Every session has a different identifier, which is sent to the client's browser as a cookie or as a $_GET variable. Sessions end when the user closes the browser, or when the web server deletes the session information, or when the programmer explicitly destroys the session. In PHP it's usually called PHPSESSID. Sessions are very useful to protect the data that the user wouldn't be able to read or write, especially when the PHP developer doesn't want to give out information in the cookies as they are easily readable. Sessions can be controlled by the $_SESSION superglobal. Data stored in this array is persistent throughout the session. It is a simple array. Sessions are much easier to use than cookies, which helps PHP developers a lot. Mostly, sessions are used for user logins, shopping carts and other additions needed to keep browsing smooth. PHP script can easily control the session's cookie which is being sent and control the whole session data. Sessions are always stored in a unique filename, either in a temporary folder or in a specific folder, if a script instructs to do so. (WIKIBOOKS)

EXAMPLE

Create file: "login.php"
<?php
@ob_start();
  
session_start(); 
 if(isset(
$_SESSION["use"]))   
   {
    
header("Location:index.php"); 
   }
 if(isset(
$_POST["login"]))   
   {
     
$user $_POST["user"];
     
$pass $_POST["pass"];
  if(
$user == "big" && $pass == "bang")   
         {                                       
      
$_SESSION["use"]=$user;  
      echo 
"<script type=`text/javascript`> window.open(`index.php`,`_self`);</script>";  
         }
        else
        {
            echo 
"WRONG!!!";        
        }
}
?>
<!DOCTYPE html>
<html>
 <head>
  <title> Login Page </title>
 </head>
<body>
username: big <br>
password: bang<br>
<form action="" method="post">
 <table width="200" border="0">
  <tr>
    <td>  UserName</td>
    <td> <input type="text" name="user" > </td>
  </tr>
  <tr>
    <td> PassWord</td>
    <td><input type="password" name="pass"></td>
  </tr>
  <tr>
    <td>
<input type="submit" name="login" value="LOGIN">
    </td>
  </tr>
 </table>
 </form>
</body>
</html> 

Create file: "logout.php"
<?php
@ob_start();
session_start();
  echo 
"Logout Successfully ";
  
session_destroy();  
  
header("Location: login.php");
?>



Create file: "index.php"

At the top of the page (be sure to first-line) add the following code:
<?php session_start();
   if(!isset(
$_SESSION["use"]))                           
       
header("Location:login.php");  
 
?>

<?php session_start();
    if(!isset(
$_SESSION["use"]))
        
header("Location:login.php");  
?>
<!DOCTYPE html>
<html>
 <head>
  <style>
 p { font-size:20px; }
</style>
 </head>
 <body>
 <h1>This is the first page</h1>
   <a href="second_page.php"><h2>NEXT PAGE</h2></a><br>
   <a href="logout.php"> Logout</a>
 </body>
</html>

Create file: "second_page.php"
<?php session_start();
      if(!isset(
$_SESSION["use"]))      
         
header("Location:login.php");  
?>
<!DOCTYPE html>
<html>
 <body>
 <h1>This is the second page</h1>
  <a href="index.php"><h2>BACK</h2></a>
  <a href="logout.php">Logout</a>
 </body>
</html>

Login system using PHP with MYSQL database (YouTube)



Privacy Policy