Hello!
Can someone please help me? My Remember me checkbox dosen’t work and since I added it everything got weird…
Everything worked fine before I added the remember me checkbox and php code for setting cookies. But now I write the email address and password to log in and I come to the user profile page but the menu dosen’t update and as soon as I go to a page that require that I’m logged in I got redirected to index.php (That happen if you ain’t logged in).
Can you guys check the code and see if you can find the problem?
Here’s the code that worked (without checkbox):
[php]
<?php include_once("php_includes/check_login_status.php"); // If user is already logged in, header that weenis away if($user_ok == true){ header("location: user.php?u=".$_SESSION["username"]); exit(); } ?><?php// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST[“e”])){
// CONNECT TO THE DATABASE
require_once(“php_includes/db_conx.php”);
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db_conx, $_POST[‘e’]);
$p = hash("SHA512", $_POST['p']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else {
// CREATE THEIR SESSIONS AND COOKIES
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);
// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
$sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
echo $db_username;
exit();
}
}
exit();
}
?>
And here’s the new code (That won’t work):
[php]
<?php require_once("php_includes/check_login_status.php"); // If user is already logged in, header that weenis away if($user_ok == true){ header("location: user.php?u=".$_SESSION["username"]); exit(); } ?><?php// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST[“e”])){
// CONNECT TO THE DATABASE
require_once(“php_includes/db_conx.php”);
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($db_conx, $_POST[‘e’]);
$p = crypt("SHA512", $_POST['p']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else if(isset($_POST['rememberme'])){
// CREATE THEIR SESSIONS AND COOKIES
setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
} else {
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
$sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
echo $db_username;
exit();
}
}
exit();
}
?>
Thanks in advance,
Busarna4