0
返回到「PHP登錄系統」測試項目之一後,我現在需要創建一個等待,以便我可以設置一個cookie,以便在用戶重新訪問網站時記住它,這可以是一個設置有或沒有記住我選項的cookie,以最好的爲準。在php登錄腳本中設置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信息。
感謝您的任何幫助和協助的先進。
簡單,你要創建一個存儲的cookie「記住我」選項,準確? – 2012-02-09 03:20:41
你正在哈希密碼,而不是'加密'它。另外,如果安全性對您的項目很重要,那麼您可以(也應該)比md5做得更好。嘗試使用:http://www.openwall.com/phpass/ – ajnatural 2012-02-09 03:26:07
@Frederick Marcoux - 是的,簡單地說,我想創建一個cookie來存儲信息以記住該網站的訪問者。 – 2012-02-09 13:18:24