无法向 $_SESSION 添加新值
我对会话变量有一个非常奇怪的问题。 该变量是一个关联数组。我在一个包含很多可过滤项目的页面(页面 A)中创建了这个。我将过滤器保存在会话变量中,
$_SESSION['filter'] = Array ( 'm' => 'acrylic', 'a' => 'P', 'c' => 1960 );
用户可以转到详细信息页面(页面 B),但
$_SESSION['filter'] = Array ( 'm' => 'acrylic', 'a' => 'P');
奇怪的是,当我进入详细信息页面时,我错过了关联数组中的最后一项 所以我无法返回正确的过滤器信息。
我在此函数中构建会话变量,选项在 URL 示例中传递: http://www.web.com/artworks/aP/c-1960/o-private+collection
带有此 URL 的参数 $args 将是此数组('aP', 'c-1960 ', 'o-private+collection')
private function filter($args){
// options
$f = array('a','c','u','t','m','o');
$a = array();
foreach($args as $i){
$t = explode('-', $i);
if (in_array($t[0], $f)){
$a[$t[0]] = urldecode($t[1]);
$this->suffix .= '/'.$i;
}
else if(is_numeric($i))
$a['pp'] = intval($i);
}
$_SESSION['filter'] = $a;
return $a;
}
我在页面 A 中调用此函数,在页面 BI 中不调用此函数,唯一的交互是
if (isset($_SESSION['filter'])){
print_r($_SESSION);
...
有人可以帮助我吗? 谢谢
I've a very strange problem with a session variable.
This var is an associative array. I create this in a page (page A) with a lot of filterable items. I save filters in a session var
$_SESSION['filter'] = Array ( 'm' => 'acrylic', 'a' => 'P', 'c' => 1960 );
the user can go to a detail page (page B) but here I have
$_SESSION['filter'] = Array ( 'm' => 'acrylic', 'a' => 'P');
the strange is that when I go i the detail page I miss the last item in the associative array
so I can't go back with the right filter info.
I build the session var in this function, the options are passed in the URL example: http://www.web.com/artworks/a-P/c-1960/o-private+collection
the argument $args with this URL will be this array('a-P', 'c-1960', 'o-private+collection')
private function filter($args){
// options
$f = array('a','c','u','t','m','o');
$a = array();
foreach($args as $i){
$t = explode('-', $i);
if (in_array($t[0], $f)){
$a[$t[0]] = urldecode($t[1]);
$this->suffix .= '/'.$i;
}
else if(is_numeric($i))
$a['pp'] = intval($i);
}
$_SESSION['filter'] = $a;
return $a;
}
I call this in page A, In the page B I don't call this function the only interaction is
if (isset($_SESSION['filter'])){
print_r($_SESSION);
...
Someone can help me?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太清楚,但尝试在引号中给出最后一个变量值。
I don't know exactly but try with giving last varible value in quotes.
您必须在某个地方调用
session_start
在将新值添加到$_SESSION
之前,在脚本中添加新值,否则它们将不会保留。你这样做吗?You have to call
session_start
somewhere in your script before adding new values into$_SESSION
, otherwise they will not persist. Do you do that?