10.3 Setting the Session Name
TechniqueUse 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"; ?> CommentsThe 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. |