我一直在使用这段代码,但不知道出了什么问题。
它可以登录,但不会将 id 传递到会话中。它适用于 JQuery。
$loginresult = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$loginresult->bindParam(":username", $username);
$loginresult->bindParam(":password", $encryptedPassword);
$loginresult->execute();
$result = $loginresult->fetch(PDO::FETCH_OBJ);
if($loginresult !== false){
$_SESSION['loggedin'] = microtime();
$_SESSION['userid'] = $result->id;
echo 'true';
}else{
echo 'false';
}
编辑:新代码
<?php
require('../../config.php');
$username = $_POST['username'];
$password = $_POST['password'];
$encryptedPassword = crypt($password,$salt);
$loginresult = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$loginresult->bindParam(":username", $username);
$loginresult->bindParam(":password", $encryptedPassword);
$loginresult->execute();
$count = $loginresult->rowCount();
var_dump($count);
print_r($db->errorInfo());
if($count){
$result = $loginresult->fetch(PDO::FETCH_OBJ);
var_dump($result);
print_r($db->errorInfo());
session_start();
$_SESSION['loggedin'] = microtime();
$_SESSION['userid'] = $result->id;
echo $result->id;
//echo 'true';
}else{
echo 'false';
}
?>