Jump to content

[SOLVED] Help with logging out


Trium918

Recommended Posts

I am able to login but everytime I log off

I get the else statement saying that I never

logged on.

 

else
  {
    // if they weren't logged in but came to this page somehow
    echo "You were not logged in, and so have not been logged out.<br>"; 
  }

 

 

<?
  session_start();

  $old_user = $valid_user;  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

<h1>Log out</h1>
<? 
  if (!empty($old_user))
  {
    if ($result)
    { 
      // if they were logged in and are not logged out 
      echo "Logged out.<br>";
    }
    else
    {
     // they were logged in and could not be logged out
      echo "Could not log you out.<br>";
    } 
  }
  else
  {
    // if they weren't logged in but came to this page somehow
    echo "You were not logged in, and so have not been logged out.<br>"; 
  }
?> 
<a href="session.php">Back to main page</a>

Link to comment
https://forums.phpfreaks.com/topic/45955-solved-help-with-logging-out/
Share on other sites

This is the code that logs the user in

 

 

<?
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log in Page</title>
</head>
<body>

<?
  if(!($_POST['username']) && !($_POST['password']))
  {
?>

  <h1>Log in</h1>
  <form action="" method="post">
   <table width="200" border="1" cellspacing="0" cellpadding="0">
   <tr>
      <td>Username:</td>
      <td><input type="text" name="username"  /></td>
   </tr>
   <tr>
      <td>Password:</td>
      <td><input type="text" name="password" /></td>
   </tr>
   <tr>
      <td align="center" colspan="2"><input type="submit" value="Log in" /> </td>
   </tr>
   </table>
  </form>

<?
  }
else
  {
    // register_global = Off
    // inside the php.ini configuration file
    $username = stripslashes($_POST['username']);
    $password = stripslashes($_POST['password']);

    // Connect to MySql
   $db = mysql_connect("localhost") or die(mysql_error());

    // Select the appropriate database
   mysql_select_db("member_auth") or die(mysql_error());

   // query the database to see if there is a record which matched
   $query = "SELECT count(*) FROM user_info_auth WHERE
		  username ='$username' AND 
		  password ='$password' ";
		  
   $result = mysql_query($query);
   if (!$result)
        echo " Cannot run query!!";
   if (mysql_num_rows($result) > 0)
    {
       //if they are in the database register the id
       $valid_user = $username;
       session_register("valid_user");
    }

   if(session_is_registered("valid_user")){
        echo "You are logged in as: $valid_user <br>";
        echo "<a href=\"logout.php\">Log out</a><br>";
   }
   else{
   if(isset($username)){
       //if they've tried and failed to log in
       echo " Could not log you in";
    }
   }
  }  
?>

 

2 scripts supplied don't even match!!

 

 


This session.php where they login 

<?
session_start();
?>

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1
/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log in Page</title>
</head>
<body>

<?
  if(!($_POST['username']) && !($_POST['password']))
  {
?>

Code:
  <h1>Log in</h1>
  <form action="" method="post">
   <table width="200" border="1" cellspacing="0" cellpadding="0">
   <tr>
      <td>Username:</td>
      <td><input type="text" name="username"  /></td>
   </tr>
   <tr>
      <td>Password:</td>
      <td><input type="text" name="password" /></td>
   </tr>
   <tr>
      <td align="center" colspan="2"><input type="submit" value="Log in" /> </td>
   </tr>
   </table>
  </form>

<?
  }
else
  {
    // register_global = Off
    // inside the php.ini configuration file
    $username = stripslashes($_POST['username']);
    $password = stripslashes($_POST['password']);

    // Connect to MySql
   $db = mysql_connect("localhost") or die(mysql_error());

    // Select the appropriate database
   mysql_select_db("member_auth") or die(mysql_error());

   // query the database to see if there is a record which matched
   $query = "SELECT count(*) FROM user_info_auth WHERE
		  username ='$username' AND 
		  password ='$password' ";
		  
   $result = mysql_query($query);
   if (!$result)
        echo " Cannot run query!!";
   if (mysql_num_rows($result) > 0)
    {
       //if they are in the database register the id
       $valid_user = $username;
       session_register("valid_user");
    }

   if(session_is_registered("valid_user")){
        echo "You are logged in as: $valid_user <br>";
        echo "<a href=\"logout.php\">Log out</a><br>";
   }
   else{
   if(isset($username)){
       //if they've tried and failed to log in
       echo " Could not log you in";
    }
   }
  }  
?>

  This the logout.php
<?
  session_start();

  $old_user = $valid_user;  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

<h1>Log out</h1>
<? 
  if (!empty($old_user))
  {
    if ($result)
    { 
      // if they were logged in and are not logged out 
      echo "Logged out.<br>";
    }
    else
    {
     // they were logged in and could not be logged out
      echo "Could not log you out.<br>";
    } 
  }
  else
  {
    // if they weren't logged in but came to this page somehow
    echo "You were not logged in, and so have not been logged out.<br>"; 
  }
?> 
<a href="session.php">Back to main page</a>

Erm... yeah

 

by doing the following

<?
  session_start();

  $old_user = $valid_user;  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

 

your removing referance that they were logged in, then your trying to check that referance

change

       //if they are in the database register the id
       $valid_user = $username;
       session_register("valid_user");

to

       //if they are in the database register the id
       $valid_user = $username;
       session["valid_user"];

 

 

 

and change

 

<?
  session_start();

  $old_user = $valid_user;  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

 

to

<?
  session_start();

  $old_user = session["valid_user"];  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

I re-register the variable $valid_user is that

appropriate because I was getting syntex errors

the other way.

 

 
<? 
session_start();

  $old_user = $valid_user;  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();
?>

to

<?
  session_start();

  $old_user = session_register("valid_user");  // store  to test if they *were* logged in
  $result = session_unregister("valid_user");
  session_destroy();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.