! Whitey !
Baseband Member
- Messages
- 23
I am trying to write a logging in and out script using PHP and cookies.
It logs a user in fine, but then they can't log out!
On the log in page basically it looks to see if there is a cookie on the users machine with their username and password, and if there is it take them directly to the members area.
When logout is clicked it is meant to modify the cookie so the time is in the past and therefore won't work because its expired.
But its just taking the user to the login page and then back to the members area.
Here is the code:
Log in page:
Log out page:
After trying (almost) everything, I'm don't know if it's the "if(isset($_COOKIE['ID_my_site'])) {" part. Is this just checking there is a cookie and not checking if it is in date?!
If it is can someone tell me how I could check the cookie exists and is in date?!
Thanks,
Dave
It logs a user in fine, but then they can't log out!
On the log in page basically it looks to see if there is a cookie on the users machine with their username and password, and if there is it take them directly to the members area.
When logout is clicked it is meant to modify the cookie so the time is in the past and therefore won't work because its expired.
But its just taking the user to the login page and then back to the members area.
Here is the code:
Log in page:
PHP:
// Check to see if there is a login cookie
if(isset($_COOKIE['ID_my_site'])) {
// check username and password and if they match go to members area
// else show log in form
// if the login form is submitted
if (isset($_POST['submit'])) {
// check username and password is correct
// set the cookie
$hour = time() + 3600; // 1 hour (60 x 60)
setcookie(ID_my_site, $username, $hour);
setcookie(Key_my_site, $pass, $hour);
// redirect to members area
// else show log in form
Log out page:
PHP:
$past = time() - 120; // minus 120 seconds ago
$msg = "gone";
setcookie(ID_my_site, $msg, $past);
// redirect to log in page
header("Location: ../login.php");
After trying (almost) everything, I'm don't know if it's the "if(isset($_COOKIE['ID_my_site'])) {" part. Is this just checking there is a cookie and not checking if it is in date?!
If it is can someone tell me how I could check the cookie exists and is in date?!
Thanks,
Dave