title: logging out in php ( cookies and sessions )
body: <p>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.</p>
  
  <p>1.the log in php code:</p>
  
  <pre><code>    &lt;?php
  session_start();
  require_once('login/connectvars.php');
  $msg = "";
  
  if(!isset($_SESSION['username']) &amp;&amp; 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) &amp;&amp; !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..';
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>2.the code i used in HTML to check the session.</p>
  
  <pre><code>   &lt;div id="login_form"&gt;
            &lt;h2&gt;
              &lt;?php echo $msg; ?&gt;
            &lt;/h2&gt;
         &lt;?php
         if(isset($_SESSION['username']))
          {
              echo 'nothing&lt;br /&gt;';
              echo '&lt;a href="login/logout.php"&gt;log out'.$_SESSION['username'].'&lt;/a&gt;&lt;br /&gt;';
          }
          else
          {
         ?&gt;
           &lt;form id="login"  method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
              &lt;table&gt;
                    &lt;tr&gt;
                       &lt;th&gt;Username:&lt;/th&gt;
                       &lt;th&gt;Password:&lt;/th&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                       &lt;td&gt;&lt;input type="text" name="username" /&gt;&lt;/td&gt;
                       &lt;td&gt;&lt;input type="password" name="password" /&gt;&lt;/td&gt;
                     &lt;/tr&gt;
                     &lt;tr&gt;
                        &lt;td&gt;&lt;input type="submit" name="submit" value="Log In" class="submit" /&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;a href="login/signup.php"&gt;&lt;input type="button" class="submit" value="Sign Up" /&gt;&lt;/a&gt;&lt;/td&gt;
                     &lt;/tr&gt;
              &lt;/table&gt;
           &lt;/form&gt;
           &lt;?php
           }
           ?&gt;
      &lt;!-- login form --&gt;
  
     &lt;/div&gt;
  </code></pre>
  
  <p>3.The logout script</p>
  
  <pre><code>    &lt;?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/");
  ?&gt;
  </code></pre>