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
Default

I assume you have some background in programming??

The first place to start is the PHP manual.
http://www.php.net/session

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..
http://www.php.net/manual/en/languag...predefined.php

$_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.

Code:
<?php

    // session.php
    // Any PHP script that requires access to session data 
    // must make a call to session_start().
    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...

Code:
<?php
    // session_2.php
    // start the session
    session_start();

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


Code:
This causes an error
<?php

    echo "This also causes an error";

    session_start();

?>
You can also Google for more examples.
http://www.google.com/search?q=PHP+session+tutorial

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote