CSE134A LECTURE NOTES

May 2, 2001
 
 

ANNOUNCEMENTS

The midterm will be on Wednesday May 16.
 
 

COOKIES

int setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])


This defines a cookie to be sent along with the rest of the header information. Cookies must be sent before any other headers are sent (this is a restriction of cookies, not PHP). This requires you to place calls to this function before any <html> or <head> tags.

All the arguments except the name argument are optional. If only the name argument is present, the cookie by that name will be deleted from the remote client. You may also replace any argument with an empty string ("") in order to skip that argument. The expire and secure arguments are integers and cannot be skipped with an empty string. Use a zero (0) instead. The expire argument is a regular Unix time integer as returned by the time() or mktime() functions. The secure indicates that the cookie should only be transmitted over a secure HTTPS connection.

Common Pitfalls:

SESSION MANAGEMENT

The information here is taken from Session Handling with PHP 4 by Tobias Ratschiller at  http://www.zend.com/zend/tut/session.php?print=1

Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term "session" refers to the time that a user is at a particular web site. The problem is, that HTTP has no mechanism to maintain state. Individual requests aren't related to each other. The Web server can't easily distinguish between single users and doesn't know about user sessions. Session management refers to the way that associate data with a user during a visit to a Web page. This tutorial uses the term session for a single visit of a user. For example, a typical online shopping session might include logging in, putting an item into the shopping cart, going to the checkout page, entering address and credit card data, submitting the order, and closing the browser window. PHP 4.0 includes native session management functions to ease the task of managing user sessions.

The "life" of a session refers to the amount of time the session is active. "Serializing" means the transformation of variables to a byte-code representation that can be stored anywhere as a normal string. Without the serializing feature, it wouldn't be possible, for example, to store PHP arrays into a database. Serializing data is very useful for preserving data across requests, an important facet of a session library. You can use serialize() and deserialize(), but note that in PHP 3 these functions don't work correctly on objects (classes); class functions will be discarded.

Serialize() returns a string containing a byte-stream representation of value that can be stored anywhere.  This is useful for storing or passing PHP values around without losing their type and structure.  To make the serialized string into a PHP value again, use unserialize(). Serialize() handles the types integer, double, string, array (multidimensional) and object in PHP 4.

PHP's session management library offers the key characteristics required of a session management library:

To associate session data with a user, you need a session identity number: a key that ties the user to his data. PHP 4.0's session management frees you from the task of inventing session IDs, serializing and storing session data.
 
Starting a Session
A PHP 4 session is started either explicitly by session_start(), or implicitely by registering a variable for the session, using session_register(). Usually, you will call session_start() on top of the page, so that session variables are available to your script, and register variables to the session later in the script. It wouldn't make a difference though, if you registered your session variables with session_register() in the head of the script and left out the session_start() call - session_register() calls session_start() internally, if the session isn't started yet. When you start a session either way, the following happens: Registering a session variable is done through the session_register() command. This allows you to create (register) variables which are stored throughout the session, and can be referred to during the session. All variables you want to preserve across page requests must be registered to the session library with the session_register() function. Note that this function takes the name of a variable as argument, not the variable itself. You can use session_unregister() to remove variables from the session, for example, when the user removes a product item from the shopping cart.

This is an example of a counter.

session_start();
print($counter);
$counter++;
session_register("counter");

Session_register() takes the name of a variable as argument, not the variable itself - this is easily confused, but the examples show the difference clearly.

It's as easy to handle session variables as it is to handle GET/POST variables. If you register a variable named foo, $foo is accessible automatically after calling session_start(). Because the serialize() function was improved in PHP 4, it's also feasible to treat objects (classes) as session variables.
 

Ending a Session
Session ending is not automatic, because it is difficult for the system to tell when the user is finished the session. Several commands help you control how the system determines when to end a session for a user. If you don't use cookies but pass the session ID via GET or POST instead, you need to pay special attention to the garbage collection routines. Users might bookmark URLs containing the session ID, so you need to make sure that sessions are cleared frequently. If the session data still exists when the user accesses the page with the session ID at a later time, PHP simply resumes the previous session instead of starting with a new session, which may not be your intention. A value of 10 to 20 for gc_probability would better fit this scenario than the default value of 1.
 
You might ask yourself why PHP allows you to specificy a probability (gc_probability) which determines when garbage collection will occur, rather than a function which cleans up every n times. If PHP used a counting function, the server would need to track the number of opened sessions somehow. Using the probability function means that the server does not have to store counters for cleanup, which translates to cleaner and faster execution.
Session ID Propagation
PHP 4 sessions support the following methods of passing the session ID: Cookies are the default way to pass the session ID between pages. If you're happy with cookies, you don't have to worry about any special configuration. Another common way is to pass the ID is with GET/POST. Your URL would then be similar to script.php3?<session-name>=<session-id>. You can create such URLs by using the global constant SID:

printf('<a href="script.php?%s">Link</a>', SID);

Automatic URL rewriting is one of the very cool new features of PHP 4, allowing you to add the session ID to all the links within the page. To enable it, you need to configure PHP with --enable-trans-id and recompile it. Then the session ID in the form = will be added to all relative links within your PHP-parsed pages. While this is a handy feature, it should be used with caution on high-performance sites. PHP has to look at each individual page, analyze it to see whether it contains relative links, and eventually add the ID to the links. This obviously introduces a performance penalty. Cookies, on the other hand, are set only once, avoiding the overhead of URL rewriting.
 
 



Copyright (c) by Charles Elkan, 2001.