PHP 会话的未定义索引 [英] Undefined index with PHP sessions

查看:20
本文介绍了PHP 会话的未定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在加载 index.php 时出现以下错误:

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

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

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

我尝试过以多种方式修改我的文本,但始终无法解决错误.

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

索引.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(提交注册表后)

Validate.php (after submitting register form)

    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(提交登录表单后)

Loginvalidate.php (after submitting login form)

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();
}

好的,所以我的脚本执行了预期的操作.我唯一无法解决的是这些会话错误.您是否看到任何滥用会话的情况?当然,我已经在所有 .php 文件中启动了会话.

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.

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

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.

推荐答案

出现这些错误的原因是您正在尝试读取不存在的数组键.isset() 函数在那里,因此您可以对此进行测试.对于每个元素,如下所示的内容将是一种享受;不需要 null 检查,因为您永远不会将 null 分配给元素:

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:

// 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']);
}

你也可以循环使用它:

$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]);
    }
}

这篇关于PHP 会话的未定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆