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

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

问题描述

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



我在加载index.php时遇到以下错误:



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

已尝试以多种方式修改我的文本,但我从来没有解决错误。



Index.php



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

Validate.php(提交注册表单后)

  if(mysqli_num_rows($ result)> 0){//如果数据库中有电子邮件的密码
$ _SESSION ['registered'] =Email已经注册。;
mysqli_close($ db_handle);
header('Location:index.php');
exit();
}

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

  if($ numrows!= 0)//如果在数据库中有邮件的密码
{
if($ row ['password'] == $ password){//如果密码与用户输入匹配
header('Location:homepage.php');
回声lol;
exit();
}
else {
$ _SESSION ['badlogin'] =电子邮件/密码组合无效。;
mysqli_close($ db_handle);
header('Location:index.php');
exit();
}
}
其他{//没有邮件的密码,所以电子邮件没有注册
$ _SESSION ['neverused'] =电子邮件没有注册。 ;
mysqli_close($ db_handle);
header('Location:index.php');
exit();
}

好的,我的脚本完成了它打算做的事情。唯一不能解决的是这些会话错误。你有没有看到滥用会话?当然,我已经开始了所有.php文件中的会话。



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

造成这些错误的原因是,重新尝试读取不存在的数组键。 isset()函数在那里,所以你可以测试这个。对于每个元素,下面的内容都会起作用;您不需要进行空检查,因为您从未将null指定给元素:

  //检查'registered'键是否存在
if(isset($ _ SESSION ['registered'])){

//它确实;输出消息
echo $ _SESSION ['registered'];

//删除密钥,以便我们不会输出消息
unset($ _ SESSION ['registered']);
}

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

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

//遍历键来测试
foreach($ keys为$ key){

//测试$ _SESSION全局数组中是否存在$ key
if(isset($ _ SESSION [$ key])){

//它确实;输出值
echo $ _SESSION [$ key];

//删除密钥,所以我们不会继续输出消息
unset($ _ SESSION [$ key]);
}
}


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.

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

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.

Index.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)

    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)

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.

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.

解决方案

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

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

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

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