简体   繁体   English

索引为变量的$ _SESSION不停留

[英]$_SESSION with index as variable not staying

I am setting a $_SESSION variable with variable index but it does not hold. 我正在使用变量索引设置$_SESSION变量,但它不成立。

if (isset($_GET['reply']))
{
 echo  $_SERVER['QUERY_STRING']; // example output: reply&55
 $vals = explode('&',$_SERVER['QUERY_STRING']); // val[1] = 55
 session_start();
 $_SESSION[strval($vals[1])] = 1;  //tried as int first and now convert to string
 print_r($_SESSION);

 }

when i look at the contents of $_SESSION it shows: Array ( [iamloggedIn] => 1 [userID] => 5 [55] => 1 ) 当我查看$ _SESSION的内容时,它显示: Array ( [iamloggedIn] => 1 [userID] => 5 [55] => 1 )

It is showing two variables set with normal string indexes (eg $_SESSION['userID'] = 5) , and then the last session variable just set. 它显示了两个使用常规字符串索引设置的变量(例如$_SESSION['userID'] = 5) ,然后设置了最后一个会话变量。

Now if this if statement is called again with a new value in val[1], say 59 for example, it overwrites the last one and my print_r will show something like: Array ( [iamloggedIn] => 1 [userID] => 5 [59] => 1 ) 现在,如果再次使用val [1]中的新值(例如说59)调用此if语句,它将覆盖最后一个,我的print_r将显示如下内容: Array ( [iamloggedIn] => 1 [userID] => 5 [59] => 1 )

It is not creating a new session variable. 它没有创建新的会话变量。

Is this because I am using a variable as the index? 这是因为我使用变量作为索引吗? I tried the index variable as an integer first and now as a string but the result is the same. 我首先尝试将index变量作为一个整数,现在尝试作为一个字符串,但是结果是相同的。

The $_SESSION variable must be an associative array, thus $ _SESSION变量必须是一个关联数组,因此

$_SESSION['33'] = 'foo'; $ _SESSION ['33'] ='foo';

Won't work unfortunately, because '33' despite being a string will be casted as an integer key of the array. 不幸的是,它将无法正常工作,因为尽管字符串为'33'也会被强制转换为数组的整数键。 For more details on how keys are automatically casted: 有关如何自动投射键的更多详细信息:

http://us.php.net/manual/en/language.types.array.php http://us.php.net/manual/en/language.types.array.php

Strings containing valid integers will be cast to the integer type. 包含有效整数的字符串将转换为整数类型。 Eg the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer. 例如,键“ 8”实际上将存储在8以下。另一方面,“ 08”将不是强制转换,因为它不是有效的十进制整数。

Floats are also cast to integers, which means that the fractional part will be truncated. 浮点数也被强制转换为整数,这意味着小数部分将被截断。 Eg the key 8.7 will actually be stored under 8. 例如,密钥8.7实际上将存储在8以下。

Bools are cast to integers, too, ie the key true will actually be stored under 1 and the key false under 0. 布尔也被强制转换为整数,即,键true将实际存储在1以下,而键false将存储在0以下。

Null will be cast to the empty string, ie the key null will actually be stored under "". 空值将强制转换为空字符串,即键空值实际上将存储在“”下。

Judging only by what you are showing us, you are right, print_r($_SESSION) should be displaying: 仅从您向我们展示的内容来看,您是正确的, 应该显示print_r($_SESSION)

Array([iamloggedIn] => 1, [userID] => 5, [55] => 1, [59] => 1)

I'd try concatenating a string before the index, like this: 我会尝试在索引之前连接一个字符串,如下所示:

$_SESSION['example' . strval($vals[1])] = 1;

And accessing it through $_SESSION['example55'] , for example. 并通过$_SESSION['example55']进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM