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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制