robcrozier Posted November 19, 2006 Share Posted November 19, 2006 Does anyone know if it is possible at all to delete all session variables within a session except maybe on or two specified ones. In other words i want to be able to delete like 30 session variables in the easiest possible way. i thought maybe you could execute a statement that deletes all session variables within the session except one or two that you could specify in this statement. Link to comment https://forums.phpfreaks.com/topic/27773-session-problem/ Share on other sites More sharing options...
Orio Posted November 19, 2006 Share Posted November 19, 2006 [code]<?php//the function recieves an array of session keys not to deletefunction delete_session ($dont_delete){ foreach($_SESSION as $key => $val) { if(!in_array($key, $dont_delete)) unset($_SESSION[$key]); }}?>[/code]You need to give the funtion an array with the values you dont want to unset, can be an empty array.Orio. Link to comment https://forums.phpfreaks.com/topic/27773-session-problem/#findComment-127055 Share on other sites More sharing options...
kenrbnsn Posted November 19, 2006 Share Posted November 19, 2006 If you know which session variables you want to save, save them in temporary variables, use the [url=http://www.php.net/session_unset]session_unset()[/url] function to unset all the session variables and then recreate the one's you saved.For example:[code]<?phpsession_start();$save_these = array('ses_var_1','ses_var_2','last_saved');foreach ($save_these as $k) $save_these[$k] = $_SESSION[$k];session_unset();foreach ($save_these as $k => $v) $_SESSION[$k] = $v;?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/27773-session-problem/#findComment-127056 Share on other sites More sharing options...
robcrozier Posted November 19, 2006 Author Share Posted November 19, 2006 chers guys, they both seem to do the job!!! ;D Link to comment https://forums.phpfreaks.com/topic/27773-session-problem/#findComment-127060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.