未定义的索引:user_email ..阅读所有其他答案。仍然无法弄清楚

时间:2017-02-17 07:27:57

标签: php session

<?php
    include("includes/connections.php");

    if(isset($_POST['sign_in'])) {


        $password = mysqli_real_escape_string($con,$_POST['pass']);
        $email = mysqli_real_escape_string($con,$_POST['email']);


        $get_user = "SELECT * FROM users WHERE user_email = '$email' AND user_password = '$password'";
        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if ($check==1) {

            $_SESSION['user_email']=$email;
            echo "<script>window.open('welcome.php','_self');</script>";

        }
        else {

            echo "<script>alert('Password or email is not correct');</script>";
            echo "<script>window.open('index.php','_self');</script>";

        }
    }

?>

有一天我开始学习php。我被困在这里。代码似乎很好,但它不会在注册或登录时启动会话。它说未定义的索引:user_email

<h1>welcome : <?php echo $_SESSION['user_email']; ?></h1>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您忘了将session_start()放在页面顶部。

答案 1 :(得分:0)

在'check = 1'处的代码中,您需要输入以下代码行:session_start();

如果您收到错误,会话已经启动,您需要将此行代码放在文档的顶部:ob_start();

<?php
 ob_start(); //Put ob_start(); here on this line


include("includes/connections.php");

        if(isset($_POST['sign_in'])) {


        $password = mysqli_real_escape_string($con,$_POST['pass']);
        $email = mysqli_real_escape_string($con,$_POST['email']);


        $get_user = "SELECT * FROM users WHERE user_email = '$email' AND user_password = '$password'";
        $run_user = mysqli_query($con,$get_user);

        $check = mysqli_num_rows($run_user);

        if ($check==1) {
            session_start(); //Put session_start(); on this line
            $_SESSION['user_email']=$email;
            echo "<script>window.open('welcome.php','_self');</script>";

        }
        else {

            echo "<script>alert('Password or email is not correct');</script>";
            echo "<script>window.open('index.php','_self');</script>";

        }
    }

&GT;