<tutorialjinni.com/>

PHP Session by Example

Posted Under: PHP, Programming on Apr 9, 2011
When run application on your computer, the computer know who you are, what are you doing, what data you alter, when you close your application and so forth, this is called session. In web communication we use Hyper Text Transfer Protocol (HTTP), which is a stateless protocol that does not retain information on server and treat all request as independent request. To make it remember user information we use SESSION.

There are many ways to retain this information, most common are
  • Session Management via Cookies
  • Embedding Session information in URLs
  • Storing Information on server in $_SESSION array (in PHP)
and many more... every scheme has its own advantages and disadvantages, In this tutorial we will make session using $_SESSION and do it using an example, say making a user log-in page, please note; session variables hold information about one single user, and are available to all pages in one application.

To start a session our page has its first line to session_start(); this tell the server to initialize $_SESSION array if it is already started it will make it available it to page. We can add any number or variable to this array, in our example we will make an simple HTML page and display user a greeting if he is log-in else we will show him a log-in form.

now Let the Code Talk


<?php
session_start();
error_reporting(0);

// CHECK if form is submitted
if(isset ($_POST["submit"])){
    $username=$_POST["username"];
    $password=$_POST["password"];

    if($username=="originative" && $password=="tutorialjinni"){
        $_SESSION["isLoggedIn"]=1;
        //setting a varable so that later i know
        // that this user i logged in
        $_SESSION["uname"]=$username;
        //setting another variable
    }
}
// for logout or destroy session
if(isset($_GET["action"])){
    if($_GET["action"]){

        session_unset();
        //unset all session variables

        session_destroy();
        // destroy session
    }
}

?>
<!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>PHP Session Login Example | Tutorial Jinni | Originative Systems</title>
</head>
    <body>
        <?php
            if(!$_SESSION["isLoggedIn"]==1){
                // checking the value of isLoggedIn
        ?>
        <form action="" method="post">
          <table width="400" border="0" cellpadding="8" cellspacing="0">
            <tr>
              <td align="right">User Name : </td>
              <td><label for="username"></label>
              <input name="username" type="text" id="username" size="35" /></td>
            </tr>
            <tr>
              <td align="right">Password : </td>
              <td><label for="pass"></label>
              <input name="password" type="password" id="pass" size="35" /></td>
            </tr>
            <tr>
              <td colspan="2"><input type="submit" name="submit" id="submit" value="Let me In" /></td>
            </tr>
          </table>
        </form>
        <?php
            }
            else{
                echo "Welcome <b><em>".$_SESSION["uname"]."</em></b>
                    To Logout Click <a href='sess.php?action=logout'>Here</a>";
            }
        ?>
    </body>
</html>



© Tutorial Jinni