Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - Using Sessions in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Using Sessions in PHP
By: Hermawan Haryanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 21
    2002-11-21

    Table of Contents:
  • Using Sessions in PHP
  • Basic Sessions
  • Running Membership with Sessions
  • Another Example
  • Conclusions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Sessions in PHP - Basic Sessions


    (Page 2 of 5 )

    <?php
    // page1.php
    session_start();
    $_SESSION["real_name"] = "Hermawan Haryanto";
    print "<a href='page2.php'>Go to this page</a>";
    ?>

    <?php
    // page2.php
    session_start();
    print $_SESSION["real_name"];
    ?>

    The above example shows you that I create a session variable named "real_name" storing my name as the value. Then on the second page, I print out the session variable and my name will show up.

    Let’s do another example:

    <?php
    session_start();
    $count = $_SESSION["counter"] + 1;
    $_SESSION["counter"] = $count;
    print $_SESSION["counter"];
    ?>

    When you first access that page, it will display 1. Try to refresh the page and the number will grow.

    To destroy or delete an existing session variable, you can use the unset command.

    unset($_SESSION["session_name"]);

    Or if you want to delete all session variables (and the session itself), you can do it by using the destroy command.

    session_destroy();

    The destroy command is usually used to log-off a user from the membership area. Let’s make a membership area for our example.

    More Miscellaneous Articles
    More By Hermawan Haryanto


       · Thank you Hermawan for a great Sessions tutorial. When I first started using...
       · I cant get this to work.. if i put in my username and password it just clears the...
       · This will be very useful to me. With slight modification, I think I can secure my...
       · Horasss SnowStorm...!!!Might be you have REGISTER GLOBAL problems. check your...
       · In step 2 of the 'Another Example' part there is a link to dbal to download. However...
       · Your web host may not allow you to connect to your session save path. Try using...
       · In login.php, the line:Header("Location: ./index.php").....
       · How to count all the sessions created for a site?
       · Check the form properties
       · If only I saw your message earlier...Anyway, for a beginner who just tried out...
       · i have most of it working by using output buffers. But in the login.php you...
       · Thanks alot for this article. I can now move ahead with my project.
     

    MISCELLANEOUS ARTICLES

    - Install Slackware on Your Old PC
    - Firefox Plugins You`re Not Using (and Should...
    - Working with MP3 ID3 Tags in FTP Server Usin...
    - How Switching to Linux Can Make Your Computi...
    - Set Up Your Home Office on Linux: a Guide fo...
    - Putty File Transfer Commands in SSH Protocol
    - Setting Up Ubuntu for Your Home Office
    - Installing Mint Linux
    - Crucial Traits of Awk
    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek