PHP Sessions

Discussion in 'PHP' started by pradeep, Oct 2, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Building web applications with membership management is one of the most frequent tasks that every programmer does. Managing membership data, such as username, password and the member's profile with sessions in PHP is the easiest and simplest solution.

    Sessions stores data about a particular user that you need on all of the pages in a variable and access it from any page. Using sessions you could hold data about particular user and access that info on all the pages as long as the user is active on the site.

    Each session holds different values for different user. Sessions automatically die when the user's session is over or he closes his browser.

    PHP's session-handling features, was first introduced in the 4.0 release, Sessions are native and thus require no additional packages.

    To work with Sessions we will be using session_start() and session_register() functions, its important that you add sesions_start to every page where you would like to use sessions.
    session1.php
    PHP:
    <?
     
    // create a new session
     
    session_start();
     
     
    // register a session-variable
     
    session_register("color"); //we have created a session with name color
     
     
    $color "blue"//assigning some value to our session
     
    ?>
    Okay now we have created a session with name color and assigned it as blue, now in our second program (session2.php) we access the value of our session color.
    session2.php

    PHP:
    <?
     
    session_start();
     
     echo 
    "The value stored in your session: " $color//retriving value from the session
     
    ?>
    To delete a session variable we use the function session_unregister() so to delete our session color we would use the code.
    PHP:
    <?
     
    session_unregister(color);
     
    ?>
    This was a very simple tutorial, we can use sessions more effectively.
    Happy Coding.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    To determine the validity of session in each page you can use

     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Correct!
    But before doing that you must invoke the function
    PHP:
    session_start();
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice