10.0 Introduction


HTTP is a stateless protocol, meaning that after a user leaves your Web page or your application ends, the computer loses all memory of the transactions that have occurred (unless you count the Web server's log files). Starting with Netscape 3.0, Netscape addressed this problem with cookies. Cookies are files that are stored on the user's computer, and are accessible to the script that sent them. They enable you to save and access information about past visits a user might have made to your Web site.

By combining cookies and databases, you could save every bit of information you have about a user on your computer and access it by using a unique ID that you assigned to the user and put in a cookie that was stored on the user's computer. However, this was very programming intensive and required a lot of time to start up.Therefore, to make your life easier, the PHP development group came out with a tightly integrated set of session management utilities for PHP. What used to take anywhere from twenty to hundreds of lines of code now takes, at most, a few lines of well-crafted code.

How It Works

Sessions work in the following manner. When the session_start() or session_ register() function is called, PHP loads the saved session data from the session store. During a script's execution, you can register variables to be saved into the session store by calling the session_register() function. When your PHP script finishes execution, the session variables are saved to the session store in the path specified by the session.save_path entry in the php.ini file. A unique ID that is associated with the session is put in a cookie and given to the user, but this happens only the first time a session is started.

Serially Though

One way of maintaining state is by using the sessions module. But in some cases, you don't need the power of the sessions module ”you just need to save and retrieve certain variables. If this is the case, you can use either PHP's serialization functions ( serialize() and unserialize() ) or you can use the bundled WDDX extension, which places PHP variables in a standard format that can then be read and used by other languages.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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