在我的站點上使用登錄系統時,所有內容都可以平穩運行,但用戶名和用戶標識的會話數據未被寫入。會話數據未被寫入
這裏是我的代碼...我拿出驗證部分,因爲它沒有任何關係與未寫入會話數據。
<?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和笑聲,看看是否沒有幫助。 – ScoPi
var_dump $ user_id和$ dbusername請。 – sunny
顯示您正在調用會話值的頁面。 – sunny