回到“PHP登录系统”测试项目之一后,我现在需要创建一个等待,以便我可以设置一个cookie,以便用户在重新访问网站时被记住,这可能是一个使用或不使用“记住我”选项设置的cookie,以最佳者为准。
这是php编码,用于在用户输入登录信息后对其进行身份验证。
<?php
session_start(); // Create the session
if (empty($_REQUEST['username']) || empty($_REQUEST['password'])) { header("Location: /login.php?username=" . $_REQUEST['username'] . "&message=0"); exit;
} else {
$username = $_POST['username']; // Gets the username
$password = $_POST['password']; // Gets the password.
$salt = "n4tOqSYcZI5Y463oG34T7YR8Q72hVU"; // random string
// Add some salt
$salt .= $password;
$password = $salt; // new salted pass.
// Encrypt the password.
$password = md5($password);
include('settings.php');
include('dbconnect.php');
$query = "SELECT * FROM xxx WHERE username = '$username' LIMIT 1";
$username = mysql_real_escape_string($username); // just to be sure.
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$resusername = $row['username'];
$respassword = $row['password'];
$resemail = $row['email'];
}
// valid user?
if ($respassword == $password) {
// Yes they are.
$_SESSION['loggedin'] = "1";
$_SESSION['email'] = $resemail;
$_SESSION['username'] = $resusername;
$SQL = "UPDATE xxx SET usr_lastlogin='" . date("Y-m-d H:i:s") . "',lastlogin_ip='" . addslashes($_SERVER['REMOTE_ADDR']) . "' WHERE username = '$_SESSION[username]'";
$result = @mysql_query($SQL) or die("Error Process 2");
header('Location: /index.php');
} else {
// No
$_SESSION['loggedin'] = "0";
header("Location: /login.php?username=" . $_REQUEST['username'] . "&message=3");
exit; }
}
?>
我需要从哪里开始,通过获取该代码来创建存储在用户计算机上的cookie信息,无论是否记住我选项,这都是最好的。
感谢您提供任何帮助和帮助。
答案 0 :(得分:0)
只需使用setcookie()并稍后从$ _COOKIE中读出来。看看setcookie(),就使用$ _COOKIE而言,它与会话几乎相同:
function isLoggedIn(){
if($_COOKIE['loggedin'] == true or $_SESSION['loggedin'] == true){
return true;
}
return false;
}