FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need help making logout script





DanielXP
I have a members area script were people can register login and all that but i can't get them to logout becoz i cann't delete the session

the login code it

Code:
<?php
include("config.php");
echo "<br>";
echo "<form method='post' action='?do=check'><table>";
echo "<tr><td>username:</td><td><input type='text' name='username'></td></tr>";
echo "<tr><td>password:</td><td><input type='password' name='password'></td></tr>";
echo "<tr><td></td><td><input type='submit' value='submit'></td></tr>";
echo "</table></form><br>";

if($_GET['do'] == 'check')
{
$_user_name = $_POST['username'];
$_password = $_POST['password'];
//check the login
mysql_connect($host, $user_name, $password)
   or die('Could not connect: ' . mysql_error());

//select database
mysql_select_db($database_name) or die('Could not select database');
$result = mysql_query("SELECT * FROM users WHERE username='$_user_name'") or die(mysql_error());
$row = mysql_fetch_array( $result );

if($row['username'] == '')
{
echo "<b>username or password is incorrect</b><br>";
}
else
{
if($row['password'] == $_password)
{
$_SESSION['logged'] = "agk8gjf38834j2";
$_SESSION['user'] = $_user_name;
header("location:" . $redirect_url);
}
else
{
echo "<b>username or password is incorrect</b><br>";
}
}

}
?>


and the logout code that don't work is

Code:
<?php
include("config.php");
$_SESSION['logged'] = '';
header("location:" . $_login_file);
?>


Can any one help me make this able to logout please Very Happy Thanks
BlackSkad
you can't just set the content of the $_SESSION to '', as you might have noticed. Instead, you can do it this way:

Code:
function logout () {
   session_unset();
   if (session_destroy()) {
      header ('Location: ' . $login_page);
   } else {
      echo "Logout failed";
   }
}


I hope this helped Smile

EDIT: moved the session_unset outside the if-statement and changed the + into a . (I have seen to much Java lately)
DanielXP
This does not kill the sesion or redirect Sad
Atomo64
You might want to use session_unset() instead; but I remember of having a similar problem some time ago; but I think that it was just an error in an old PHP version,.
Rhysige
WTH is with if(SESSION_DESTROY()) ???

I use two methods in one to ensure its deleted.

Code:

function logout(){
   $_SESSION = array();
   session_destroy();
   redirect('index.php');
}


Remember that you must have your own redirect function a simple one for that is.

Code:

function redirect($url){
    if(substr($url,0,4) == 'http')
    {
       //Absolute link
       header('Location: ' . $url);
    } else {
       //Relative
       if($url{0} == '/'){
          header('Location: http://'.$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . $url);
       } else {
          header('Location: http://'.$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '' . $url);
       }
    }
    exit;
}

Any problems just ask Smile
BlackSkad
It's weird that it doesn't work. I use it multiple projects and it seems to work alright.
session_destroy returns a boolean (true or false). True is returned when the session is destroyed, false when it's not. Most of the time that return-value is ignored, but I use it...

But ok, a new version. Moved the session_unset outside the if-statement and changed the '+' into an '.' for concatenating strings.

Code:
function logout () {
   session_unset();
   if (session_destroy()) {
      header ('Location: ' . $login_page);
   } else {
      echo "Logout failed";
   }
}


BlackSkad
Related topics
PHP tables
Flash - Photo Gallery - need help please.
PHP login/logout problem
Need help with php script
Help making an AJAX messaging system
I have some scripts but don't know what to do with them?
Rap Website
Need help with PHP script[RESOLVED]
Need help making flash music player..
Need help with logger script. (half done)
need help with a script for vb its not to hard i think
.: User login systems for your site :. CHEAP AND EASY!
cookies help - - - Urgent help
session_destroy problems
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.