View Single Post
  #5 (permalink)  
Old July 21st, 2004, 08:13 AM
richard.york's Avatar
richard.york richard.york is offline
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

I assume you have some background in programming??

The first place to start is the PHP manual.

That's the technical, and very dry explaination of each session function.

Sessions are a way to store and persist data between client connections. Its not a permenent storage receptical, like say a database is. Its a way to store data that's used while the visitor is using the website, once the user closes their browser, the session ends and at some point after that garbage collection happens, which is PHP's process of deleting outdated session files. Therefore, sessions are temporary.

In PHP every session is initiated with a call to session_start() at the very beginning of the PHP script. This function does the following:

1. It issues a cookie in the HTTP response headers to the client's browser. The cookie contains the session id, which associates the client with the session data that's stored on the server.
2. It creates a plain text file on the server, where session data is to be stored. (See previous post).
3. If a session already exists, it opens that session data file and populates session variables with the data contained in the plain text file. This data is contained in the $_SESSION superglobal variable.

Here is an explaination of special variables like $_SESSION..

$_SESSION is an array, therefore you use array syntax to create variables and to access them.

Here is a very simple demonstration of a session in action.


    // session.php
    // Any PHP script that requires access to session data 
    // must make a call to session_start().

    $_SESSION['foo'] = 'This data persists between connections.';

    echo "<a href='session_2.php?sid='".session_id()."'>Go to the next page.</a>\n";

Then the second file...

    // session_2.php
    // start the session

    // This variable still exists!
    echo $_SESSION['foo'];
Finally because session_start() alters the outgoing HTTP headers, it may not have output before it.

This causes an error

    echo "This also causes an error";


You can also Google for more examples.



The Spicy Peanut Project
Reply With Quote