简体   繁体   English

传递SESSION变量,出现错误

[英]Passing a SESSION variable, getting errors

I am trying to pass a session variable from one page to another but it seems liek the session variable is empty on the next page. 我正在尝试将会话变量从一页传递到另一页,但似乎该会话变量在下一页上为空。 Thanks for the help! 谢谢您的帮助!

Here is the entirety of the code on the two pages. 这是两页上的全部代码。

mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_sitename);
$pagename= $row_getDisplay['USER_NAME']; 
$query=mysql_query("SELECT * FROM COMMENTS WHERE COMMENT_ON='$pagename' LIMIT 0, $no_of_comments");
echo "<hr />";

echo "<h3>Latest Comments</h3>";
session_start();
$count = 0;
while($fetch=mysql_fetch_array($query)) 
{

  echo "<p>".$fetch['COMMENT']."<a href=delete_comment.php?count=".$count.">X</a><br/><sub><b>Comment by: </b>".$fetch['COMMENT_BY']."</sub><hr /><p>";
  $_SESSION[strval($count)]=$fetch['COMMENT'];
  //echo $_SESSION[strval($count)];
  $count = $count + 1;

  print_r($_SESSION);
}
mysql_close();

delete_comment.php delete_comment.php

<?php
session_start();
print_r($_SESSION);
error_reporting(E_ALL);
ini_set('display_errors', '1');


$count = $_GET['count'];
echo $count;
$comment = $_SESSION[strval($count)];

$db_sitename="genydb";
$db_hostname="localhost";
$db_username=
$db_password=

$con = mysqli_connect($db_hostname, $db_username, $db_password, $db_sitename);

echo $comment;
$myquery = sprintf("DELETE FROM COMMENTS WHERE COMMENT=%s", GetSQLValueString($_SESSION[$_GET['count']], 'text'));
$mysql=($myquery) or die(mysql_error());

mysqli_close($con);

?>

This is the error 这是错误

Notice: Undefined offset: 0 in /var/www/geny/bootstrap/delete_comment.php on line 8
$comment = $_SESSION[strval($count)];

I believe it is because you are using a numeric key with $_SESSION. 我相信这是因为您在$ _SESSION中使用数字键。 I tried it out locally and I got this notice on the first page: 我在本地尝试过,并且在第一页上收到了此通知:

Notice: Unknown: Skipping numeric key 0 in Unknown on line 0

I saw this bug report as well after getting that notice https://bugs.php.net/bug.php?id=42472 接到通知后,我也看到了此错误报告https://bugs.php.net/bug.php?id=42472

If you do something like this it doesn't raise the notice and it passes the variable properly: 如果您执行以下操作,则不会引起注意,它会正确传递变量:

$_SESSION['keyName'][strval($count)]=$fetch['COMMENT'];

Edit: Thanks to Wiseguy for turning me on to this bit of info. 编辑:感谢Wiseguy为我提供了这一信息。

His comment below is: 他在下面的评论是:

According to this answer , "keys in the $_SESSION array are subject to the same limitations as regular variable names in PHP, ie they cannot start with a number". 根据此答案 ,“ $ _ SESSION数组中的键与PHP中的常规变量名称受相同的限制,即它们不能以数字开头”。 I guess this was a side effect of register_globals. 我猜这是register_globals的副作用。 Curiously, the docs no longer state this. 奇怪的是,文档不再说明这一点。 Perhaps this limitation was removed when the register_globals option was removed in PHP 5.4.0 在PHP 5.4.0中删除register_globals选项时,可能已删除了此限制。

To elaborate on that a little bit the reason it didn't work with register_globals is because of how PHP treats super global variables such as $_SESSION. 要详细说明一下,它不能与register_globals一起使用的原因是因为PHP如何处理诸如$ _SESSION之类的超全局变量。 One of the things that makes a super global special is that it exists in all scopes without needing to use the 'global' keyword. 使超级全局特性与众不同的一件事是,它在所有范围中都存在,而无需使用'global'关键字。 The other thing is that it creates variables for all of the child elements automatically... again in all scopes. 另一件事是,它会自动为所有子元素创建变量……同样在所有作用域中。 So if you have something like 所以如果你有类似的东西
$_SESSION['username'] = 'Levi';
That would create an additional variable $username which is equal to 'Levi'. 这将创建一个附加变量$ username,它等于“ Levi”。 So in the example above when you set a 因此,在上面的示例中,当您设置
$_SESSION[1] = 'Test';
PHP is going to try and set a variable $1 = 'Test'; PHP将尝试设置变量$ 1 ='Test'; Which is not possible 这是不可能的

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

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