Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - Using Sessions in PHP
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap