会话值未在PHP中设置

时间:2018-07-04 05:18:59

标签: php

index.php

<?php
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];
        $password = $_POST['password'];
        $sql = "select * from admin where email = '$email' and password = '$password'";
        $result = mysqli_query($con,$sql);
        $num_rows = mysqli_num_rows($result);
        if($result)
        {
            if ($num_rows > 0) 
            {
                $sqls = "select * from admin where email = '$email' and password = '$password'";
                $results = mysqli_query($con,$sqls);
                while ($rows = mysqli_fetch_assoc($results)) 
                {
                    $_SESSION['admin_id'] =  $rows['id'];
                }
                header ("Location: dashboard.php");
            }
            else 
            {
                echo "<p id='red'>Wrong email or password.</p>";
            }   
        }
    }
?>

dashboard.php

<?php
    session_start(); 
    include('../config.php');
    $admin_id = $_SESSION['admin_id'];
    echo $admin_id;
?>

在此代码中,当我在index.php中回显$ _SESSION ['admin_id']时,我创建了登录表单并将id初始化为会话变量,它返回值,但当我要在仪表板中设置$ _SESSION ['admin_id']时。 PHP的,但它不工作或没有价值。如何解决此问题?请帮助我。

谢谢

4 个答案:

答案 0 :(得分:1)

添加session_start();到index.php的顶部(当然在php标签内)

 session_start(); 

答案 1 :(得分:0)

session_start()是否已在index.php文件中初始化?您应该先检查一下。建议将其放置在脚本的所有文件或单个包含文件的顶部。

session_start();

测试是否可以解决您的问题。

答案 2 :(得分:0)

在php中设置或获取会话之前,您应该始终在页面顶部(如下所示)开始会话

session_start();

因此,在您的代码中尝试在index.php中启动会话

答案 3 :(得分:0)

index.php

中添加session_start()
<?php
session_start();
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];
        $password = $_POST['password'];
        $sql = "select * from admin where email = '$email' and password = '$password'";
        $result = mysqli_query($con,$sql);
        $num_rows = mysqli_num_rows($result);
        if($result)
        {
            if ($num_rows > 0) 
            {
                $sqls = "select * from admin where email = '$email' and password = '$password'";
                $results = mysqli_query($con,$sqls);
                while ($rows = mysqli_fetch_assoc($results)) 
                {
                    $_SESSION['admin_id'] =  $rows['id'];
                }
                header ("Location: dashboard.php");
            }
            else 
            {
                echo "<p id='red'>Wrong email or password.</p>";
            }   
        }
    }
?>