Pradeep
Pradeep

Reputation: 1223

logging out in php ( cookies and sessions )

i have written login and logout scripts in php.I am able to log in but log out is not working.Someone please point the mistake.i am completely lost.Here's the complete code.

1.the log in php code:

    <?php
session_start();
require_once('login/connectvars.php');
$msg = "";

if(!isset($_SESSION['username']) && isset($_COOKIE['username']))
{
    $_SESSION['username'] = $_COOKIE['username'];
}

if(isset($_POST['submit']))
{
    $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
    $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
    $password = mysqli_real_escape_string($dbc,trim($_POST['password']));

    if(!empty($username) && !empty($password))
    {
        $query = "SELECT username FROM users_dns WHERE username = '$username' AND password = SHA('$password')";
        $data = mysqli_query($dbc,$query) or die(mysqli_error($dbc));

        if(mysqli_num_rows($data) == 1)
        {
            $row = mysqli_fetch_array($data);
            $_SESSION['username'] = $row['username'];
            setcookie('username',$row['username'],time()+(60*6));
            $msg = 'success';
        }
        else
        {
            $msg = 'enter correct values..';
        }
    }
    else
    {
        $msg = 'Enter all fields..';
    }
}

?>

2.the code i used in HTML to check the session.

   <div id="login_form">
          <h2>
            <?php echo $msg; ?>
          </h2>
       <?php
       if(isset($_SESSION['username']))
        {
            echo 'nothing<br />';
            echo '<a href="login/logout.php">log out'.$_SESSION['username'].'</a><br />';
        }
        else
        {
       ?>
         <form id="login"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table>
                  <tr>
                     <th>Username:</th>
                     <th>Password:</th>
                  </tr>
                  <tr>
                     <td><input type="text" name="username" /></td>
                     <td><input type="password" name="password" /></td>
                   </tr>
                   <tr>
                      <td><input type="submit" name="submit" value="Log In" class="submit" /></td>
                      <td><a href="login/signup.php"><input type="button" class="submit" value="Sign Up" /></a></td>
                   </tr>
            </table>
         </form>
         <?php
         }
         ?>
    <!-- login form -->

   </div>

3.The logout script

    <?php
session_start();

if(isset($_SESSION['username']))
{
    $_SESSION = array();

    if(isset($_COOKIE[session_name()]))
    {
        setcookie(session_name(),'',time() - 3600);
    }
    session_destroy();
}

setcookie('username','',time() - 3600);

echo 'redirecting you...please wait..';
header("Refresh: 3;url=http://localhost/");
?>

Upvotes: 1

Views: 6561

Answers (2)

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

For deleting all cookies try.

<?php

if (isset($_SERVER['HTTP_COOKIE']))
{
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie)
    {
        $mainCookies = explode('=', $cookie);
        $name = trim($mainCookies[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

And for session you need to learn more about ob_start(); and ob_flush();. Cheers.

Upvotes: 3

Devang Rathod
Devang Rathod

Reputation: 6736

For logout just use :

session_destroy();
header('Location: index.php'); // file name where you want to redirect after logout
exit();

Upvotes: 0

Related Questions