Hi, I've been reading this forums for a while but this is my first post here. I've done much googling before finally deciding to post for help, so this is my last option.
I'm making a login script and I've got the session to work fine, but I can't seem to get my cookie to save. I can't find my mistake either, so hopefully someone can point out what I am doing wrong?
This is my code in the login.php:
PHP Code:
$username = $_POST['username'];
$pass = $_POST['password'];
$remember = $_POST['remember'];
$result = user_login($username, $pass);
if ($result != FALSE) {
//-- Login Succesful
$password = md5($pass);
$_SESSION['stillLogged'] = '1';
$_SESSION['userName'] = $username;
$_SESSION['userPass'] = $passwrod;
if ($remember == '1') {
setcookie("cookuser_1", $_SESSION['userName'], time() + 60*3);
setcookie("cookpass_1", $_SESSION['userPass'], time() + 60*3);
}
echo 'LOGGED IN SUCCESSFULL';
} else {
//-- Login Failed
echo 'LOGGED IN FAILURE';
}
This is the login form code. It checks for session/cookie. If there isn't either set, it displays the login form. If a session is set, is shows successful login. If a cookie is set, it will use the information stored to log in automatically for the user. I removed all the html as it's not needed in my question, so I just made notifications where necessary.
PHP Code:
if((!isset($_SESSION['stillLogged'])) && (!isset($_COOKIE['cookuser_1']))) {
echo 'User not logged in. Display login form.';
} elseif (isset($_SESSION['stillLogged'])) {
$userName = $_SESSION['userName'];
echo 'You are logged in as: '. $userName;
} elseif (isset($_COOKIE['cookuser_1'])) {
$username = $_COOKIE['cookuser_1'];
$pass = $_COOKIE['cookpass_1'];
$result = user_login($username, $pass);
if ($result != FALSE) {
//-- Login Successful
$_SESSION['stillLogged'] = '1';
$_SESSION['userName'] = $username;
echo 'SUCCESSFUL LOGIN';
} else {
//-- Login Failed
echo 'LOGIN FAILURE';
}
}
Now, my sessions work fine. But once I exit my browser, go back to the login page, it should use the cookie information to log me in, but does not. It displays the login form, which means that a cookie isn't set. I've used sessions before, but I am now trying to teach myself cookies(I'm fairly new to PHP, I'm trying to self-teach).
Am I way off? or have I made a small mistake that is easily fixed? And I'm aware I only have it set for 180 seconds, this is temporary as I am just trying to get this to work.