【问题标题】:Why isn't my Session Variable working?为什么我的会话变量不起作用?
【发布时间】:2012-04-12 21:53:33
【问题描述】:

我已经检查了 php 标签之前的空白等正常内容,并且有一个 session_start() 但我无法解决这个问题。我正在为客户制作一个登录系统,所以这很重要。

基本上,一旦我进入第二页 $_SESSION['username'];是空的。 我已经打印出来了,它是空的,但是 atm 会激活标题重定向,您可以在代码中看到。

提前感谢您收到的任何帮助:)

相关代码:

<?php
session_start();

include '../resources/methods/Library.php';

if(isset($_SESSION['username']))
{
    //User already logged in!
    header('Location: Index.php');
}

//Username and password submitted by user
$usernameSubmitted = $_POST['username'];
$passwordSubmitted = $_POST['password'];

if($usernameSubmitted != "" && $passwordSubmitted != "")
{
    //User has entered both a username and a password. We shall validate them

    //Connect to database and select all the admin accounts
    connectToDB();
    $query = "SELECT * FROM admins" or die(mysql_error());
    $data = mysql_query($query) or die(mysql_error());
    $numberOfAdmins = mysql_num_rows($data) or die(mysql_error());


    //Check if the username corresponds to any found in the database                        
    $usernameValid = false;

    for($i = 0; $i < $numberOfAdmins; $i++)
    {
        if($usernameSubmitted == mysql_result($data, $i, "Username"))
        {
            $userToLogInAs = $i;
            $usernameValid = true;
        }
    }

    //If username is valid, check password
    if($usernameValid != false)
    {
        //Passwords are held as blowfish encryptions for security. Encypt this so we can compare
        $encryptedPasswordSubmitted = crypt($passwordSubmitted, '$2a$07$buzzybees5hivestottenhoe$');

        if($encryptedPasswordSubmitted == mysql_result($data, $userToLogInAs, "Password"))
        {
            //Create a session variable so the user remains logged in
            $_SESSION['username'] = $usernameSubmitted;

            //User entered the correct username and password, redirect them to the website.
            header('Location: Index.php');
        }
    }

    //If we've got this far then the user didn't authenticate successfully.
    $message = "<h2>Sorry, Invalid Credentials</h2><p>Check that you're tying your username and password correctly.</p>";
}

?>

还有下一页:

<?php
session_start();

if(!isset($_SESSION['username']))
{
    //User not signed in, send them to the log in page
    header('Location: Log-In.php');
}
?>

有什么想法吗?

谢谢, 丹尼

【问题讨论】:

  • 什么意思?
  • 你能告诉我们为什么它不起作用。错误信息?发生了什么意外?
  • 这样一个业余错误,不包括为什么它坏了,补充说。
  • 为什么不绑定只选择用户名匹配的行?
  • 是的,我已经使用了 exit('test') 并且我到达并执行了所述分支:) cbuckley,快速开发。我只是想让事情先工作,然后回去添加消毒和其他东西:)

标签: php session session-variables


【解决方案1】:

这只是一个猜测,但假设您只显示登录脚本的部分代码:

您在成功登录后重定向后不使用die(),因此之后出现的任何代码以及您未在此处显示的代码都会被执行。如果您在那里操作 $_SESSION 变量,那可能会导致您的问题。

为了安全起见,只需将代码更改为:

    if($encryptedPasswordSubmitted == mysql_result($data, $userToLogInAs, "Password"))
    {
        //Create a session variable so the user remains logged in
        $_SESSION['username'] = $usernameSubmitted;

        //User entered the correct username and password, redirect them to the website.
        header('Location: Index.php');
        die();    // this is important!
    }

看看是否能解决问题。请注意,您需要在重定向的任何地方都这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多