Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Sessions

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
Hi there,

When I'm using cookies, I just set the cookie to any information I want:

<?

$loginUserName = $HTTP_POST_VARS[loginUserName];
setcookie (&quot;CkUserName&quot;, $loginUserName, time()+14400, &quot;/&quot;, &quot;.myDomain.com&quot;,0);

?>

then if I want to retrieve that cookie, I just call it:

<?

$CkUserName = $HTTP_COOKIE_VARS[&quot;CkUserName&quot;];
echo &quot;$CkUserName&quot;;

?>

This is just Great, but if I want to use sessions, cause so many programmers advice me to use sessions instead of cookies, so:

Q. How can I convert the above 2 scripts, to use sessions instead of cookies? because I searched the php manual and I couldn't find the way.
 
Sessions are not that hard to use.

The basics:
At the beginning of any script (before the script outputs anything) issue session_start().

Any variables you wish to use through session management, add it using session_register($variable). (You only have to do this once per variable in your entire application. From then on, the variable is available everywhere.)

The session variables are available in the super-global array $_SESSION (if your PHP version is new enough to support it), the global array $HTTP_SESSION_VARS, and (if you have register globals turned on in php.ini) just the name of the variable.

If you don't want a variable to be carried by the session management system, use session_unregister() on the name of the variable.

When you're done with all your variables use session_unset() and session_destroy().



There are some other details in PHP.ini which control how long sessions last, when garbage collection on old session stores happen, etc. There are also additional functions for checking whether a variable is already registered with the session management system, setting the name of the cookie the session management system uses, etc. It's all here:
The PHP session management system stores the session variable data on the filesystem of your server and sets a cookie the value of which is basically the name of the file where your data is. This has two advantages: you only set one cookie for as many variables as you need; and the value of the cookie is meaningless outside of your server, so you don't have to worry about someone's retrieving your user's data from the cookie store on his machine.

It's also possible to use something other than the server filesystem to store session variables. I've written a FAQ in this forum which describes how to use MySQL as the datastore for sessions -- faq434-2037 . ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top