Php 通过引用取消设置会话不起作用
我正在尝试用一个函数取消会话,这样更容易。 我这样调用函数:Php 通过引用取消设置会话不起作用,php,session,reference,unset,Php,Session,Reference,Unset,我正在尝试用一个函数取消会话,这样更容易。 我这样调用函数:unsetSession(“index1/index2/index3/…) 当我提供最后一个元素的完整路径时,它就起作用了。但是,当我想删除包含更多元素的元素时,它就不起作用了 例如: $\u会话: ["test"]=> array(1) { ["value1"]=> array(1) { ["value2"]=> string(6) "String" } } 此:unset会话(“te
unsetSession(“index1/index2/index3/…)
当我提供最后一个元素的完整路径时,它就起作用了。但是,当我想删除包含更多元素的元素时,它就不起作用了
例如:
$\u会话
:
["test"]=>
array(1) {
["value1"]=>
array(1) {
["value2"]=>
string(6) "String"
}
}
此:unset会话(“test/value1/value2”)
将工作并删除value2
。
这:unsetSession(“test/value1”)
不起作用。这是我的问题
代码:
PUBLIC function unsetSession($s) {
if (!strstr($s, "/")) {
unset($_SESSION[$s]);
}
else {
$temp = &$_SESSION;
$path = explode('/', $s);
if (!isset($temp[current($path)]) OR is_string($temp[current($path)])) return false;
$temp = &$temp[current($path)];
while ($next = next($path)) {
if ((isset($temp[$next]) OR $temp[$next] == null) AND !is_array($temp[$next])) {
unset($temp[$next]);
return true;
}
$temp = &$temp[$next];
}
unset($temp); // <- DOES NOT UNSET SESSION, why?
return true;
}
return false;
}
公共功能会话($s){
如果(!strstr($s,“/”)){
取消设置($_会话[$s]);
}
否则{
$temp=&$\u会话;
$path=explode(“/”,$s);
如果(!isset($temp[current($path)])或is_string($temp[current($path)])返回false;
$temp=&$temp[当前($path)];
while($next=next($path)){
如果((isset($temp[$next])或$temp[$next]==null)和!是_数组($temp[$next])){
未设置($temp[$next]);
返回true;
}
$temp=&$temp[$next];
}
unset($temp);//对于提供数组最后一个元素的函数end(),我将替换使用current()和next():
function unsetSession($s) {
if (!strstr($s, "/")) {
unset($_SESSION[$s]);
} else {
$path = explode('/', $s)
unset(end($path));
}
return false;
}
对于提供数组最后一个元素的函数end(),我将替换使用current()和next():
function unsetSession($s) {
if (!strstr($s, "/")) {
unset($_SESSION[$s]);
} else {
$path = explode('/', $s)
unset(end($path));
}
return false;
}