如何查看用户是否登录


How to check whether the user is logged in?

我尝试在我的页面" connection .php"中验证用户是否已经登录。正常情况下,我的代码应该工作,但它没有。错误似乎出现在重定向中。这是我的代码。

x

如果我执行var_dump(),我的$_SESSION['id']确实存在,如果我有一个回显';在标题("Location: ../../Index/v/Index .php")之后,文本显示良好,因此错误似乎是在重定向中,即使它写得很好。

header()命令只有在发生任何其他数据传输之前才会实际工作,所以因为您已经从<!DOCTYPE html>输出了HTML到<body>,所以header()将被忽略。

在输出任何HTML

之前尝试检查一下
<?php
    //Ouverture session
    session_start();
    if(isset($_SESSION['id'])) {
        header("Location: ../../Index/v/index.php");
    }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Undbe</title>
    <meta charset="utf-8" />
</head>
<body>
<?php
    if(isset($_COOKIE['login_email']) && isset($_COOKIE['login_pass']))
    {
        // Soon
    }
    else
    {
?>
        <h1>Connexion</h1>
        <form method="post" action="../c/c_connexion.php">
            <label for="email">Votre email :</label> <input type="email" name="email" id="email" /><br />
            <label for="password">Votre password :</label> <input type="password" name="password" id="password" /></br><br />
            <input type="checkbox" name="remember" id="remember" /> <label>Rester connecté.</label>
            <input type="submit" value="Envoyer" />
        </form>
<?php
    }
?>
</body>
</html>