session_start() 和 $_SESSION 命令不起作用

The session_start () and $ _SESSION command is not working

我无法在 php 中启动 session_start () 我真的不明白原因,我尝试了论坛上人们建议的一些方法,但是 session_start() 还是不行

不检索变量的索引文件 $_SESSION:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Pagina1</title>
    <link rel="stylesheet" href="css/style.css"/>

</head>
<body>
    <article id="newPrincipal">
        <h1>Usuário id:<?php echo $_SESSION['userId']; ?></h1> 
    </article>
    <h1>Result:<?php echo "Usuário id:".$_SESSION['userId']; ?></h1>
</body>

</html>
<?php
echo '<pre>';
print_r($_SESSION['userId']);
echo '</pre>';

登录文件:

<?php

if(isset($_POST['login-submit'])){

    require 'dbh.inc.php';

   $users = $_POST['nome'];
    $mailuid = $_POST['mailuid'];
    $password = $_POST['pwd'];
    $token;
    if(empty($mailuid) || empty($password)){
        header("Location: ../header.php=emptyfields");
        exit();
    }
    else{
        $sql = "SELECT * FROM users WHERE Usuarios=? AND email=?";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)){
            header("Location: ../index.php?error=sqlerror");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt, "ss", $mailuid, $users);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if($row = mysqli_fetch_assoc($result)){
                $pwdCheck = password_verify($password, $row['pwdUsers']);
                if($pwdCheck == false){
                    header("Location: ../header.php?error=wrongpwd");
                    exit();
                }
                else if($pwdCheck == true){
                    session_start();
                    $_SESSION['userId'] = $row['idUsers'];
                    $_SESSION['userId2'] = $row['uidUsers'];
                    $_SESSION['email'] = $row['emailUsers'];

                    header("Location: ../index.php?login=".$_SESSION['userId']);
                }
                else{
                    header("Location: ../login.php?error=wrongpwd");
                    exit();
                }
            }
        }
    }
}
else{
    header("Location: ../index.php");
}

我使用的是外部服务器而不是 Xampp / Wamp,我不知道这是否意味着什么...

我尝试了各种方法来使用索引文件中的 $_SESSION 检索一些信息,但我做不到。

为确保您正在调用 session_start(); 并检查两个页面上可能存在的会话错误,请确保两个页面上的前三行代码如下:

<?php
error_reporting(E_ALL); //will show session errors (if any)
session_start(); //starts the session and makes $_SESSION variables available

然后,在您的登录文件中,更改设置 $_SESSION 变量的代码以匹配以下内容:

...
else if($pwdCheck == true){
    session_start();
    $_SESSION['userId'] = $row['idUsers'];
    $_SESSION['userId2'] = $row['uidUsers'];
    $_SESSION['email'] = $row['emailUsers'];

    //test output
    echo "The $_SESSION['userID'] variable is set as: " & $_SESSION['userID'];

    exit;

    //comment the redirect out for now, so that you see the test output
    //header("Location: ../index.php?login=".$_SESSION['userId']);
}
...

如果您遇到任何错误以及登录文件的测试输出是什么,请在评论中告诉我。