10.3 Setting the Session Name


10.3 Setting the Session Name

The session name is the name that is stored in users' browsers, and the name that is shown when users visit your Web site who have warnings enabled for cookies. You want to change the name of your session from the one specified in the configuration file.

Technique

Use the session_name() function to change the name of the session cookie. Remember to call this function before the session_register() and session_start() functions:

 <?php $old_session = session_name('WebsiteTracker'); // // when called with no parameters session_name // simply returns the current session name // $new_session = session_name(); // register a new session variable session_register('session_variable'); print "The old session name was: $old_session, "; print "the new session name is $new_session"; ?> 

Comments

The default value for the name of the cookie stored in the user 's Web browser is PHPSESSID , but that is not very descriptive name for a cookie. In fact, when most end users who have warnings enabled for cookies are prompted to accept a cookie named PHPSESSID , they will probably be hesitant to accept it. The session_name() function enables you to change the name of the cookie at runtime, so you don't have to make sitewide changes by altering the session.name parameter in php.ini.

When calling the session_name() function, there are a couple rules. First, the new session name must be alphanumeric . No question marks, tildes (~), and so on. Second, the session_name() function must be called before the session_start() function or session_register() function for it to have any effect.

When called with parameters, the session_name() function modifies the session name variable and returns the old session name. However, if the session_name() function is called in void context, it simply returns the name of the current session as a string.



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