Saturday, April 9, 2011

PHP Session by Example

PHP Session login example
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>


8 comments:

Anonymous said...

very good tutorial , informative, keep it up

Viktor on April 13, 2011 at 9:24 AM said...

Hi, I found one misspelled row (10.):

if($username=="originative" && $password="tutorialjinni")...


Very good tutorial, thx

Originative on April 13, 2011 at 11:00 PM said...

Thanks Viktor for pointing out, i fixed it

Anonymous said...

nice tutorial........ :)

Anonymous said...

im new for php. i use session but one warning always displayed 'Cannot send session cookie - headers already sent by'.

rply me.......

Originative on April 24, 2012 at 1:02 AM said...

such type of warning comes when you echo anything before initializing session, it may be a white space in an html document.

കുമാരര് വാസുദേവര് on February 27, 2013 at 7:38 PM said...

can u add the contents in sess.php also?

Originative on March 1, 2013 at 11:15 AM said...

you can add any type of content in session like files, object etc... but it is recommend that you put minimal data in it.

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems