会话register_globals在php中警告

时间:2013-01-23 07:07:05

标签: php

  

可能重复:
  PHP session side-effect warning with global variables as a source of data

我通过PHP从Ajax获得响应。 我收到了这个错误:

  

PHP警告:未知:您的脚本可能依赖   在PHP 4.2.3之前存在的会话副作用。   请注意,会话扩展没有   将全局变量视为数据源,除非   register_globals已启用。你可以禁用它   功能和此警告通过设置session.bug_compat_42   或者session.bug_compat_warn分别为“未知”   在第0行

我该如何解决?

我的PHP脚本是

<?php

    include("include/config.inc.php");

    $name = $_POST['loginname'];
    $phone = $_POST['logintelephone'];

    // To protect MySQL injection
    $name = stripslashes($name);
    $phone = stripslashes($phone);
    $name = mysql_real_escape_string($name);
    $phone = mysql_real_escape_string($phone);

    $query  = mysql_query("select * from chatapp_users where name = '$name' and phone_no  = '$phone'");
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($query);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count > 0){
        $result = mysql_fetch_array($query);
        session_start();
        $_SESSION['currentuser'] = $name;
        $_SESSION['currentuserid'] = $result['user_id'];
        $_SESSION['phone'] = $result['phone'];
        echo 1;
    }else {
        echo 2;
    }
?>

2 个答案:

答案 0 :(得分:0)

您可以在php.ini中设置“session.bug_compat_warn = off”。这应该关闭那个警告。

另一种解决方案是将PHP更新为高于4.2.3版的内容。

答案 1 :(得分:0)

另外两个解决方案:

  • 将您的代码放入函数中。

    函数中的变量不是(除非您在全局命名空间中使用global语句),因此不会显示此错误。

  • 不要使用与(使用过的)会话索引同名的变量。在这种情况下,请重命名$phone$_SESSION['phone']

this answer中还有其他(令人不安的)有关此警告的详细信息。