Unregistering Session Variables

   

You may find that some of the session variables that you register are only needed on a few pages and not across an entire site. You can unregister these variables so that they are no longer tracked with the session. This is done using the session_unregister() function:

 session_unregister(STRING);  

The STRING argument is the name of the variable that you want to unregister from the session. Note that using session_unregister doesn't delete the contents of the variable on the current page. However, the variable value will not be passed to other pages in the session once it has been deleted from the session file.

You may also want to first check to see if the variable is even registered by using the session_is_registered() function:

 session_is_registered(STRING);  

For example:

 //If $name has been registered with the session.  if(session_is_registered("name")) { unset($name);     session_unregister("name"); } 

This has the effect of deleting the variable from the session, as well as deleting the variable from the current page. Also, you don't have to unset the variables one at a time. You can unset all session variables by using the command session_unset():

 session_unset();  

session_unset() takes no arguments and simply deletes the values of any variables currently registered with the session. session_unregister() and session_unset() are independent of each other. However, do not call session_unregister() before session_unset() if you also want to unset the value of the variable you are unregistering. Unregistering the variable makes that variable untouchable by session_unset(), for it is no longer a session variable. The proper order to unset the variable $name, as well as unregister it, is:

 session_unset();  session_unregister("name"); 

You could also do the opposite if you wanted to make sure that a variable was registered and had a value:

 //If $name has not been registered with the session.  if(!session_is_registered("name")) { session_register("name"); $name = "Spike"; } 

   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net