Working with Sessions


When the user is working with the various pages in your web application, all the data in the various pages is reset to its original values when those pages are loaded. That means all the data in those pages is wiped out between page accesses, which is not ideal if your web application is a large one, holding many pages. To preserve that data between page accesses, you can use cookies, as we've already seen, or you can use sessions.

Sessions are designed to hold data on the server. Each user is given a cookie with his or her session ID, which means that PHP will be able to reinstate all the data in each user's session automatically, even over multiple page accesses. If the user has cookies turned off, PHP can encode the session ID in URLs instead.

Using sessions, you can store and retrieve data by name. To work with a session, you start by calling session_start. To store data in the session, you use the $_SESSION array. For example, here we're storing "blue" under the key "color":

 session_start(); $_SESSION['color'] = "blue"; 

Now in another page access (either of the same or a different page), you can access the data under the key "color" using $_SESSION again:

 session_start(); $color = $_SESSION['color']; 

In this way, you're able to preserve data between page accesses. Session support is enabled in PHP by default. If you would not like to build your PHP with session support, you should specify the --disable-session option. The Windows version of PHP has built-in support for this extension.

Session behavior is affected by these settings in php.ininote for example that you can set how long a session lasts by setting a value for session.cache_expire in minutes:

  • session.save_path "/tmp"

  • session.name "PHPSESSID"

  • session.save_handler "files"

  • session.auto_start "0"

  • session.gc_probability "1"

  • session.gc_divisor "100"

  • session.gc_maxlifetime "1440"

  • session.serialize_handler "php"

  • session.cookie_lifetime "0"

  • session.cookie_path "/"

  • session.cookie_domain ""

  • session.cookie_secure ""

  • session.use_cookies "1"

  • session.use_only_cookies "0"

  • session.referer_check ""

  • session.entropy_file ""

  • session.entropy_length "0"

  • session.cache_limiter "nocache"

  • session.cache_expire "180"

  • session.use_trans_sid "0" PHP_INI_SYSTEM | PHP_INI_PERDIR

In particular, it's important to note that all data for a particular session will be stored in a file in the directory specified by the session.save_path item. A file for each session (whether or not any data is associated with that session) will be created.

In Windows, you must set this item yourself. By default, it's set this way (change this value in Linux and Unix as well if no /tmp is available):

 session.save_path "/tmp" 

Set this item to the location you want to use for session files, which are automatically handled by PHP. For example, in Windows, if you have a PHP directory, that might look something like this:

 session.save_path "\PHP" 

That's it; we're set to use sessions. We'll put them to work starting in the next chunk.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net