在我的登录页面中,我正在使用会话和Cookie,常规登录和cookie的会话,当记住我被选中时。我创建会话或设置cookie的代码是:
if(isset($_POST['login'])){
$username = $_POST['user_login'];
$password = $_POST['password_login'];
$stmt = $db->prepare("SELECT * FROM userss WHERE username = :username AND password = :password");
$stmt->execute(array(':username'=>$username,':password'=>$password));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$user_db = $row['username'];
$pass_db = $row['password'];
if($username == $user_db && $password == $pass_db) {
$_SESSION['username']=$username;
if ($_POST['rememberme']!=NULL) {
setcookie('username', $username,time()+31556926);
}
header("Location:main.php");
}
然后在任何创建用户功能的页面上我正在调用会话或cookie:
if(isset($_COOKIE['username'])||isset($_SESSION['username'])) {
$username = $_COOKIE['username'];
$username = $_SESSION['username'];
}
现在我的问题是:
if(isset($_COOKIE['username'])||isset($_SESSION['username'])) {
是正确的,但我不确定如何为两种不同情况定义用户名
$username = $_COOKIE['username'];
给了我未定义的索引用户名。可能是我设置cookie的方式出错了。答案 0 :(得分:0)
替换
if (isset($_POST['rememberme'])) {
setcookie('username', $username,time()+31556926);
}
在用户功能中
if(isset($_SESSION['username']) {
$username = $_SESSION['username'];
}
else if(isset($_COOKIE['username']){
$username = $_COOKIE['username'];
}
else
{
//invalid ---
}