dpql57753
2018-11-06 15:38
浏览 103
已采纳

session_start()和$ _SESSION命令无法正常工作[重复]

I'm not able to start session_start () in php and I really can't understand the reason, I've tried a few things people suggested on forums, but the session_start() still does not work

index file where are not retrieving variables $ _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>';

login file:

<?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");
}

I'm using an external server not the Xampp / Wamp, I do not know if this would imply anything ...

I tried every ways to retrieve some information using $ _SESSION in the index file, but I could not.

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzongzi0379 2018-11-06 15:44
    已采纳

    To ensure that you are calling session_start(); and checking for possible session errors on both pages, make sure the first three lines of code on both pages are the following:

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

    Then, in your login file, change the code where you set the $_SESSION variables to match the following:

    ...
    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']);
    }
    ...
    

    In the comments, let me know if you get any errors and what the test output for the login file is.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题