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

时间:2018-11-06 15:38:01

标签: php html forms session login

我无法在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在索引文件中检索某些信息,但是我做不到。

1 个答案:

答案 0 :(得分:0)

要确保您正在调用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']);
}
...

在评论中,让我知道是否出现任何错误以及登录文件的测试输出是什么。