在我的网站上使用登录系统时,一切运行顺利,但没有写入用户名和用户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); ?>
答案 0 :(得分:0)
我们可以在那些有session_start()的页面中查看会话数据; 我认为你在index.php中打印会话数据,所以在index.php中必须有 在session_start();在打印输出之前。