11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 18 • SESSION HANDLERSwww.it-ebooks.infostring session_encode()This function is particularly useful when you’d like to easily store a user’s session informationwithin a database, as well as for debugging, giving you an easy way to review a session’s contents. As anexample, assume that a cookie containing that user’s SID is stored on his computer. When the userrequests the page containing the following listing, the user ID is retrieved from the cookie. This value isthen assigned to be the SID. Certain session variables are created and assigned values, and then all ofthis information is encoded using session_encode(), readying it for insertion into a database, like so:// Encode all session data into a single string and return the result$sessionVars = session_encode();echo $sessionVars;This returns:username|s:5:"jason";loggedon|s:20:"Feb 16 2011 22:32:29";Keep in mind that session_encode() will encode all session variables available to that user, not justthose that were registered within the particular script in which session_encode() executes.Decoding Session DataEncoded session data can be decoded with session_decode(). Its prototype looks like this:boolean session_decode(string session_data)The input parameter session_data represents the encoded string of session variables. The functionwill decode the variables, returning them to their original format, and subsequently return TRUE onsuccess and FALSE otherwise. Continuing the previous example, suppose that some session data wasencoded and stored in a database, namely the SID and the variables $_SESSION['username'] and$_SESSION['loggedon']. In the following script, that data is retrieved from the table and decoded:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!