【问题标题】:The session_start () and $ _SESSION command is not working [duplicate]session_start() 和 $_SESSION 命令不起作用[重复]
【发布时间】:2018-11-07 20:48:59
【问题描述】:

我在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(); 应始终位于代码的顶部
  • 您的index.php 应该会生成一个错误,因为当它运行时,$_SESSION 中将没有任何内容,因为尚未完成登录。将ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 添加到脚本的顶部。这将强制任何 mysqli_ 错误生成您可以在浏览器上看到的异常以及正常的 PHP 错误
  • Riggsfolly 和 Funk 49 感谢您的回复。我做了你要求我做的一切,但它仍然没有工作,你知道找出代码中是否有任何错误的最佳方法吗??

标签: php html forms session login


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帮助,贾斯汀。我在脚本顶部添加了 session_start (),即使它不起作用!!
  • 我刚刚再次更新了我的解决方案,我注意到另一个小错误。请试试这个,让我知道它是否有效。
  • 参数正确,我不知道为什么会话不起作用,当我要求在登录脚本中打印 $ _SESSION ['userId'] 时它可以工作,但是当我要求打印时在 sript 索引中它不起作用
  • 在您的索引文件的末尾,看起来您打开了 PHP 标记,但您没有关闭它。将结束 PHP 标记 (?&gt;) 添加到索引文件的最后。另外,尝试使用echo $_SESSION['userId']; 而不是print_r($_SESSION['userId']);
  • 没有用。无论如何,谢谢你的帮助,我放弃了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 2018-02-04
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
相关资源
最近更新 更多