Page 1 of 1

Log out link

Posted: Mon Aug 18, 2008 11:52 pm
by iebwebdesign
Hello everyone!

I'm fairly new to PHP and am working on creating a custom CMS. Right now, I have a table called "admin_users" with records of my admin users (first_name, last_name, username, password). When a user submits the login form, if the query that runs finds a username/password match in the db, I use the following code:

Code: Select all

session_start();
            $_SESSION['valid'] = "yes";
            $_SESSION['user'] = $_POST['username'];
            
            header("Location: ../index.php");
 
to start a session, set a valid to yes (which is checked on every page and if not "yes" they get redirected to the login form), and set the user name they posted to a session variable.

Now I'm trying to create a "log out" link. Here's what I did on the first try:

Code: Select all

<a href="index.php?c=logout&task=1" title="Logout">Log Out</a>
That sets a link to my logout page, which checks:

Code: Select all

if ($_GET[task] == 1 ) {
    $_SESSION[valid] = "";
    header('Location: skin/login.php');
    exit;
    }
I supposed that if $_SESSION[valid] must remain "yes" in order for the user to remain logged in, setting it to "" (nothing) would kick the user out and make him/her log in again. However, it does nothing. The user stays logged in and can still perform tasks.

What is the proper way to handle a log out using a link? Any help with this would be greatly appreciated. I'm open to any and all criticism.

Re: Log out link

Posted: Tue Aug 19, 2008 1:07 am
by eskio
Hi,

When you logout you have to kill the session.Create a page and call it for example logout.php.

Code: Select all

<?php
session_start();
// destroy session
unset($_SESSION['valid']);
unset($_SESSION['user']);
session_destroy();
// destroy cookies
$sessionPath = session_get_cookie_params(); 
setcookie(session_name(), "", 0, $sessionPath["path"], $sessionPath["domain"]); 
header('Location: login.php');
?>
Once you have killed the sesion you will be redirected to the login page.

And create a logout form which calls the logout page.

Code: Select all

<form name="frmLogout" method="post" action="logout.php">
  <input name="cmdLogout" type="submit" value=" Logout " >
</form>

Re: Log out link

Posted: Wed Aug 20, 2008 3:21 pm
by iebwebdesign
Thanks eskio! Worked like a charm!