Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Php Sessions Tutorial

Php Sessions Tutorial

Ratings: (0)|Views: 10 |Likes:
Published by bipin00

More info:

Published by: bipin00 on Jan 19, 2010
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





PHP Tutorial: SessionsBy Vince Barnes
There is a relationship between Sessions and Cookies -- they serve somewhat the same purpose,and are, to a certain extent, usable interchangeably. Sessions, which were integrated into PHP inversion 4 of the language, are a means to store and track data for a user while they travel througha series of pages, or page iterations, on your site.The most significant differences between the two are that cookies are stored on the client, while thesession data is stored on the server. As a result, sessions are more secure than cookies (noinformation is being sent back and forth between the client and the server) and sessions work evenwhen the user has disabled cookies in their browser. Cookies, on the other hand, can be used totrack information even from one session to another by setting it's time( ) parameter (seehttp://www.htmlgoodies.com/php/p14cookies.html)How Sessions WorkSessions in PHP are started by using the session_start( ) function. Like the setcookie( ) function,the session_start( ) function must come before any HTML, including blank lines, on the page. It willlook like this:<?phpsession_start( );?><html><head> ....... etcThe session_start( ) function generates a random Session Id and stores it in a cookie on the user'scomputer (this is the only session information that is actually stored on the client side.) The defaultname for the cookie is PHPSESSID, although this can be changed in the PHP configuration files onthe server (most hosting companies will leave it alone, however.) To reference the session Id inyou PHP code, you would therefore reference the variable $PHPSESSID (it's a cookie name;remember that from Cookies?)Your sharp mind may be wondering what happens when you come to the second pass through yourpage and reach the session_start( ) function again. PHP knows that there is already a session onprogress and so ignores subsequent instances of the session_start( ) -- phew!!Using Session DataHaving established a session, you can now create, store and retrieve information pertaining to thatsession. You might want, for example, to keep track of items in your visitor's shopping cart.Information for sessions is stored in a special directory on the server; the path of that directory isspecified in the server's PHP configuration files.Information to be stored for a session is kept in session variables. Session variables are created byregistering them for the session, using the session_register( ) function. To use that information (onany page iteration in the session) you simply reference the variable just like you would any othervariable. Here's an example:<?phpsession_start( );?>
<html><head><title>Using a session variable</title></head><body><?phpprint "Welcome to session number: ";print $PHPSESSID;?><br /><?phpsession_register("username");$username = "Goody";print "Your name is: ";print $username;?></body></html>In this example we have created a session and displayed the session number. We then registered asession variable called username (notice the quotes around the variable's name in the call to thesession_register( ) function.)Next we assigned a value to that variable with the " = " assignment operator (remember operatorsfrom http://www.htmlgoodies.com/php/p05expressions.html?) and then displayed the value of thatsession variable.We now have all the basic tools to establish a session, and to create and use variables that lastthrough the entire duration of the session.
PHP Tutorial: Saving Sessions in a FileBy Vince Barnes
Having created a session and saved session data for use across iterations of pages within a session,we can see that there would be one more very useful capability. What if we could save sessioninformation from one session to another, returning to information that was saved perhaps a fewdays ago? PHP provides this capability by enabling you to save session information in a file.A particularly useful example of the application of this ability, is to capture and refer to usernameand password information. You could, for example, create a login page where a visitor can entertheir user Id (their email address, perhaps) and their password, You would then retrieve sessioninformation from an earlier session, based on their user Id, and present them with customizedviews of the rest of your site, based on other stored session variables that you retrieve from thefile. Such a project would, of course, require a little more code that is shown in this tutorial, butthe methods used to save and retrieve the session data are right here!Another great example of the use of this feature is in conjunction with a shopping cart. Supposeyou have a shopping cart that builds an order as the user makes their selections and stores thatinformation somewhere based on a unique identifier such as a combination of their user Id and thesession number. Now suppose their connection is interrupted and their session is lost. If you hadsaved their original session information in a file, then when they reconnected and logged back intoyour site, you would have all the information you would need to retrieve their partially completedorder. That would make for a happy shopper!And so to the technique, which is simple enough: we're going to use the fopen function to open ourfile, the session_encode function to encode our session information into a string, the fputs functionto write it into our file and the fclose function to close the file. Here's an example:<?phpsession_register("username");session_register("password");session_register("ordernumber");$username = "Goody";$password = "mypass";$ordernumber = "1234";$sessionfile = fopen("sessionfile.txt", "w");fputs($sessionfile, session_encode( ) );fclose($sessionfile);?>As you can see, we are creating three session variables to hold the information we will need and, inthis example, are simply populating those variables from text strings. you will remember from thefile system tutorial in this series about file use of the file functions shown here. You can see herethat in the fputs instruction we are using the session_encode function to take all our sessioninformation (which includes all our session variables, but does not include the session number) andencode it into a string which becomes the information we write (fputs) to our file.Now that we have our information safely tucked away, we need a method to retrieve it again whenwe need it. For this, we'll use the fgets function to get the record and the session_decode toretrieve the session information from the record returned. Here we go:

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->