【问题标题】:Session data not being written会话数据未写入
【发布时间】:2014-03-27 04:56:43
【问题描述】:

在我的网站上使用登录系统时,一切运行顺利,但没有写入用户名和用户 ID 的会话数据。

这是我的代码...我取出了验证部分,因为它与未写入的会话数据无关。

<?php

/*** begin our session ***/
session_start();

if {

validation here
}
else
{
/*** if we are here the data is valid and we can insert it into database ***/
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);

/*** now we can encrypt the password ***/
$password = sha1( $password );

/*** connect to database ***/
include("config.php");

try
  {

    /*** prepare the select statement ***/
    $stmt = $db->prepare("SELECT userID, username, password FROM users 
                WHERE username = :username AND password = :password");

    /*** bind the parameters ***/
    $stmt->bindParam(':username', $username, PDO::PARAM_STR);
    $stmt->bindParam(':password', $password, PDO::PARAM_STR, 40);

    /*** execute the prepared statement ***/
    $stmt->execute();

    /*** check for a result ***/
    $user_id = $stmt->fetchColumn();
    $dbusername = $stmt->fetchColumn(1);

    /*** if we have no result then fail boat ***/
    if($user_id == false)
    {
            $message = 'Login Failed';
    }
    /*** if we do have a result, all is well ***/
    else
    {
            /*** set the session user_id variable ***/
            $_SESSION['user_id'] = $user_id;
            $_SESSION['username'] = $dbusername;
            session_write_close();
            header("Location: index.php");
    }


}
catch(Exception $e)
{
    /*** if we are here, something has gone wrong with the database ***/
    $message = 'We are unable to process your request. Please try again later"';
}
}
?>

这是我调用会话数据的页面。我只取出了我调用会话数据的部分,这是我唯一有 session_start() 的地方

<?php session_start();
print_r($_SESSION, TRUE); ?>

【问题讨论】:

  • 摆脱 session_write_close() 的 sh*ts 和咧嘴笑,看看是否没有帮助。
  • var_dump $user_id 和 $dbusername 请。
  • 显示您调用会话值的页面。
  • 将 '$message = 'We are ...' 替换为: echo 'Message: ' .$e->getMessage();

标签: php database session pdo


【解决方案1】:

我们可以在那些有session_start()的页面中查看会话数据; 我认为您正在 index.php 中打印会话数据,因此在 index.php 中必须有 session_start();在打印输出之前。

【讨论】:

  • 我在 index.php 上有 session_start()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
相关资源
最近更新 更多