How do I unset $_SESSION with wildcards
Example I have 100 fields of form. Each field will have error session. When form successfully submitted, I want unset any session which word begin with $_SESSION['submit_']
First I can do like
unset($_SESSION['submit_first_name'],
$_SESSION['submit_last_name'],
$_SESSION['submit_status'],
$_SESSION['submit_mobile'],
$_SESSION['su开发者_如何学Cbmit_category']);
But too long.
If I do like
session_destroy();
Then will kill my log in session
Possible to unset the session word begin with [submit_']
?
Let me know :)
You can do something like this:
foreach($_SESSION as $key=>$value)
if (strpos($key, 'submit_') === 0)
unset($_SESSION[$key]);
Or you can rearrange your $_SESSION variable like this:
$_SESSION['submit']['first_name'];
$_SESSION['submit']['last_name'];
$_SESSION['log']['some_log'];
.
.
.
unset($_SESSION['submit']);
Not sure if you can use wildcards, but you can list them out in one command if that helps you... Like so:
unset($_SESSION['submit_last_name'], $_SESSION['submit_status'], $_SESSION['submit_mobile'], etc...);
But I'm not sure if that gets you anywhere... At least it's not on multiple lines. I will research using wildcards, but you can Google as well as I can :)
精彩评论