asmith Posted December 13, 2007 Share Posted December 13, 2007 is there any php function to unset all the variabes together? (and maybe all but 1 or 2 ) i can unset a variable i had set with : unset($_SESSION[aaa]); i want to know if i have for example 20 variables stored in session like above , how can i unset all without having to code 20 times "unset()" . and if i want to unset 18 of them and keep 2 , what should i do ? Quote Link to comment Share on other sites More sharing options...
jitesh Posted December 13, 2007 Share Posted December 13, 2007 session_unset (PHP 4, PHP 5) session_unset -- Free all session variables Description void session_unset ( void ) The session_unset() function frees all session variables currently registered. <?php session_unset(); ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted December 13, 2007 Author Share Posted December 13, 2007 thanks for the tip , i read the php.net session unset too ! but i didn't get how to keep some session variables while unsetting others ? in previous example how can i keep $_SESSION[aaa] and unset all of the other session variabels ? what about keeping more than one ? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 13, 2007 Share Posted December 13, 2007 I wrote a user function for you since I do not think there is any php function to suit your needs function fnUserSessionUnset($arrKeepVars=array()) { foreach ($_SESSION as $strKey => $strValue) { if (!in_array($strKey,$arrKeepVars)) { unset($_SESSION[$strKey]); } } } so if you like to keep 'aaa' and 'bbb' just use it like this fnUserSessionUnset(array('aaa','bbb')); hope its helpful Quote Link to comment Share on other sites More sharing options...
asmith Posted December 13, 2007 Author Share Posted December 13, 2007 yea it will work ! i understand what you did there ! i don't know what to say ! maybe i should thank you in a different way ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.