带有 PHP 会话的未定义索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18731693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:06:31  来源:igfitidea点击:

Undefined index with PHP sessions

phpsessionif-statementheader

提问by LPB

I'm new to PHP and am even more of a beginner when it comes to sessions. I have my index.php page, which is where users can register and login. The forms are posting to validate.php and loginvalidate.php pages, respectively for registering and logging in.

我是 PHP 的新手,在会话方面更像是一个初学者。我有我的 index.php 页面,这是用户可以注册和登录的地方。表单分别发布到validate.php 和loginvalidate.php 页面,用于注册和登录。

I have these errors on index.php when I load it:

当我加载 index.php 时,我有这些错误:

1) Notice: Undefined index: registered 2) Notice: Undefined index: neverused

1) 注意:未定义索引:已注册 2) 注意:未定义索引:从未使用

I have tried modifying my text in many ways but I never got to solve the errors.

我曾尝试以多种方式修改我的文本,但我从未解决过错误。

Index.php

索引.php

    <?php
            if ($_SESSION['registered'] != NULL){
                echo $_SESSION['registered'];                   
            }
            if ($_SESSION['badlogin'] != NULL){
                echo $_SESSION['badlogin'];
            }
            if ($_SESSION['neverused'] != NULL) {
                echo $_SESSION['neverused'];                    
            }
    ?>

Validate.php (after submitting register form)

Validate.php(提交注册表后)

    if (mysqli_num_rows($result) > 0) {  //IF THERE IS A PASSWORD FOR THAT EMAIL IN DATABASE
    $_SESSION['registered'] = "Email is already registered.";
    mysqli_close($db_handle);
    header('Location: index.php');
    exit();
}

Loginvalidate.php (after submitting login form)

Loginvalidate.php(提交登录表单后)

if ($numrows!=0)  //IF THERE IS A PASSWORD FOR THAT EMAIL IN THE DATABASE
{
  if ($row['password'] == $password) {  //IF THE PASSWORD MATCHES USER INPUT
      header('Location: homepage.php');
      echo "lol";
      exit();
  }
  else{
    $_SESSION['badlogin'] = "Email/password combination not valid.";
    mysqli_close($db_handle);
    header('Location: index.php');
    exit();
  } 
}
else {  //THERE IS NO PASSWORD FOR THAT EMAIL, SO THAT EMAIL IS NOT REGISTERED
    $_SESSION['neverused'] = "Email is not registered.";
    mysqli_close($db_handle);
    header('Location: index.php');
    exit();
}

Okay so my script does what it is intended to do. The only thing that I can't solve is these session errors. Do you see any misuse of sessions? Of course, I have started the sessions in all of my .php files.

好的,所以我的脚本完成了它的意图。我唯一无法解决的是这些会话错误。你看到任何滥用会话的情况吗?当然,我已经在我所有的 .php 文件中启动了会话。

Also, note that I am aware that there is no protection from hackers. This is only for a future prototype that won't contain any important data.

另外,请注意,我知道没有针对黑客的保护措施。这仅适用于不包含任何重要数据的未来原型。

回答by George Brighton

The reason for these errors is that you're trying to read an array key that doesn't exist. The isset()function is there so you can test for this. Something like the following for each element will work a treat; there's no need for null checks as you never assign null to an element:

这些错误的原因是您试图读取不存在的数组键。该isset()函数的功能是存在的,所以你可以测试这个。对于每个元素,类似于以下内容的内容将是一种享受;不需要空检查,因为您永远不会将空分配给元素:

// check that the 'registered' key exists
if (isset($_SESSION['registered'])) {

    // it does; output the message
    echo $_SESSION['registered'];

    // remove the key so we don't keep outputting the message
    unset($_SESSION['registered']);
}

You could also use it in a loop:

您也可以在循环中使用它:

$keys = array('registered', 'badlogin', 'neverused');

//iterate over the keys to test
foreach($keys as $key) {

    // test if $key exists in the $_SESSION global array
    if (isset($_SESSION[$key])) {

        // it does; output the value
        echo $_SESSION[$key];

        // remove the key so we don't keep outputting the message
        unset($_SESSION[$key]);
    }
}

回答by TeejMonster

If you're getting undefined index errors, you might try making sure that your indexes are set before you try comparing the values. See the documentation for the isset function here: http://php.net/manual/en/function.isset.php

如果您遇到未定义的索引错误,您可以尝试在尝试比较值之前确保您的索引已设置。请参阅此处isset函数的文档:http: //php.net/manual/en/function.isset.php

if (isset($_SESSION['registered']))
  if ($_SESSION['registered'] != NULL){
      echo $_SESSION['registered'];                   
  }
}
if (isset($_SESSION['badlogin']))
  if ($_SESSION['badlogin'] != NULL){
      echo $_SESSION['badlogin'];
  }
}
if (isset($_SESSION['neverused']))
  if ($_SESSION['neverused'] != NULL) {
      echo $_SESSION['neverused'];                    
  }
}