After going back to one of ‘PHP Login System’ test projects, I now need to create a wait so that I can set a cookie so that the user is remembered when they re-visit the website, this can either be a cookie that is set with or without a remember me option, whichever is best.
Here is the php coding that authenicates a user after they’ve entered their login information.
<?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; }
}
?>
Where would I need to start, with getting that code to also create the cookie information that is stored on a users computer, either with or without a remember me option, which ever is best.
Thanks for any help and assistance in advanced.
Simply use setcookie() and read it out of $_COOKIE later. Have a look at setcookie() and as far as using $_COOKIE goes it’s pretty much the same as session: