PHP会话将无法正常工作?在多个页面上使用变量

用户名

我想要的是我可以使用一个变量在多个页面上使用。
唯一的问题是,它不起作用,因为我做错了我相信的事情。
经过几个小时的研究,我认为可能是什么问题,所以我决定将其发布在这里,以便您可以为我提供帮助。
我的代码:
login.php:

<?php
    if(empty($_SESSION['pass']))  { 
        echo "U bent niet ingelogt.";
        echo '<form action="index2.php" method="post">';
        echo 'Code <input type="text" name="code">';
        echo '<input type="submit">';
        echo '</form>';
    } else {
        redirect($url);
    }

    $url = "index2.php";
    function redirect($url)
    {
        if (headers_sent()) {
            die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
        } else {
            header('Location: ' . $url);
            die();
        }    
    }
?>

Index2.php:

<?php 
    session_start();
    // error_reporting(0);

    $CODE = $_POST["code"];
    $_SESSION["pass"] = $CODE;
    if ($CODE == "testpassword") {
        echo "password correct";
        echo '<br /><a href="index.php">Index</a>';
    } else {
        echo "Password wrong";
        echo '<br /><a href="login.php">Login</a>';
        exit();
    }
?>

Index.php:

<?php 
    session_start();
    // error_reporting(0);

    $CODE = $_POST["code"];
    $_SESSION["pass"] = $CODE;
    if ($CODE == "testpassword") {
        echo "password correct";
    } else {
        echo "Password wrong";
        exit();
    }
?>
马塞尔·巴尔泽(Marcel Balzer)

您需要session_start()在每个使用$_SESSION[]变量的页面上

在第一个文件中丢失。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章