1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Using Sessions in PHP

Discussion in 'PHP' started by lionaneesh, Mar 3, 2011.

  1. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    Every web-applications needs to transfer data from one page to another and one of the way of doing it is using sessions in PHP..

    Sessions are a special type of variables used to transfer/carry data from one page to others.. In this tutorial i’ll be covering PHP version 4.1.0 and above..

    Using sessions in PHP



    Before using sessions in php we have to use a function to initialise ..
    Code:
    session_start();
    
    The function initialises PHP Session array $_SESSION..and gets its values...Without this function the $_SESSIONS[] array cannot be used..

    To assign a session a variable..
    We simply use :-
    Code:
    $_SESSION[key] =value;
    
    Eg :-
    Code:
    $_SESSION[“name”] = “lionaneesh”;
    
    So , that’s quite a bit of theory now lets get coding..
    Code:
    <?php
    session_start();
    ?>
    <html>
    <head>
      <title>Demonstrating the use of Session Variables in PHP</title>
    </head>
    <body>
    <?php
    $_SESSION["name"] = "lioaneesh";
    print"I have set a session variable 'name' to ". $_SESSION['name'] . "<br />\n";
    print"<a href=\"next.php\">Please Click Here!!</a>\n" 
    ?>
    </body>
    </html> 
    
    next.php
    PHP:
    <?php
    session_start
    ();
    ?>
    <html>
    <head>
      <title>Demonstrating the use of Session Variables in PHP</title>
    </head>
    <body>
    <?php
    $name 
    $_SESSION["name"];
    print
    "The name passed to the page using Session variables is $name\n"
    ?>
    </body>
    </html>
    Output :-
    session.php
    Code:
    I have set a session variable 'name' to lioaneesh
    Please Click Here!!
    
    next.php
    Code:
    The name passed to the page using Session variables is lioaneesh
    
    Now that we know how to create and use sessions..
    What if we want to unset a session variable or delete the Whole $_SESSION array (for cleanup purposes)..
    We can unset a session variable simply by using :-
    Code:
    unset($_SESSION[key]);
    
    We can destroy the session by using :-
    Code:
    session_destroy();
    
    Let’s just use them in our code and see what’s happening
    session.php
    Code:
    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Demonstrating the use of Session Variables in PHP</title>
    </head>
    <body>
    <?php
    $_SESSION["name"] = "lioaneesh";
    $_SESSION["site"] = "Go4expert";
    print"<a href=\"next.php\">Please Click Here!!</a>\n" 
    ?>
    </body>
    </html>
    
    next.php
    Code:
    <?php
    session_start();
    ?>
    <html>
    <head>
    <title>Demonstrating the use of Session Variables in PHP</title>
    </head>
    <body>
    <?php
    $name = $_SESSION["name"];
    $site = $_SESSION["site"];
    print"The name passed to the page using Session variables is $name\n<br />";
    print"The site name passed to the page using Session variables is $site\n<br />";
    
    print"Unsetting name\n<br />";
    
    unset($_SESSION["name"]);
    
    print $_SESSION['name'];
    
    print "Destrying the session\n<br />";
    
    session_destroy();
    
    ?>
    </body>
    </html>
    
    Output :-
    Code:
    The name passed to the page using Session variables is lioaneesh 
    The site name passed to the page using Session variables is Go4expert 
    Unsetting name
    Notice: Undefined index: name in D:\wamp\www\testiNg\next.php on line 19
    Destroying the session
    
    Stay tuned for more..
     
  2. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    Thanks for accepting..
     
  3. etrade123

    etrade123 Banned

    Joined:
    Mar 14, 2011
    Messages:
    10
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    seo
    Location:
    Delhi, india
    Home Page:
    thanks for giving some important tips.
     
  4. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    Welcome and have a look at my other articles toO!!
     
  5. etiergjt

    etiergjt New Member

    Joined:
    Mar 21, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    I'm a newbie in php,before today I'm puzzuled with session,now I understand it.Do you have articles about cookie?
     
  6. eunikorn

    eunikorn New Member

    Joined:
    Mar 28, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Is it possible to utilize php session superglobals to have file based or database based server side session management. Is it possible to create web pages that actually do not require any rewriting of code if in future session management backend is changed ? Like in case we are switching to a load balancing proxy, we would like to switch from file based to database based session management.
     
  7. muralideni

    muralideni New Member

    Joined:
    Oct 18, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i need login/logout script in php mysql. I done it bt didnt work properly.Once logged in the user dont go back to the login form without click logout link. i need help someone help me............. muralideni
     
  8. donor

    donor Banned

    Joined:
    Jan 12, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    web developer
    Location:
    india
    plz anybody tellme there is an error in this code????
    <?php
    session_start();
    if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
    else
    $_SESSION['views'] = 1;

    echo "views = ". $_SESSION['views'];
    ?>
     
  9. jamesjohn

    jamesjohn Banned

    Joined:
    Aug 12, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Development
    Location:
    San Jose, CA
    Home Page:
    Wow..very valuable information you have shared here.
    Thanks a lot.
     
  10. lionaneesh

    lionaneesh New Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    My Pleasure :)
     
  11. vnexpress

    vnexpress New Member

    Joined:
    Jun 11, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thank, it very well, i learning PHP
     

Share This Page