title: Undefined index: when using php session
body: <p>I am following this tutorial: <a href="http://www.youtube.com/watch?v=mn0ucCuNOTI" rel="nofollow">http://www.youtube.com/watch?v=mn0ucCuNOTI</a>.</p>
  
  <p>The part that is wrong is that I have created a form where you have logged in with the following code and that code seems to work:</p>
  
  <pre><code>&lt;?php include_once("db.php"); 
  session_start();
  ?&gt;
  &lt;?php
      $user = $_POST['name'];
      $pass = $_POST['pwd'];
  
       $sql="SELECT count(*) from phplogin WHERE(username='$user' and password='$pass')";
  
      $query = mysql_query($sql);
  
      $result = mysql_fetch_array($query);
  
      if($result[0]&gt;0){
      $_SESSION['username']=$user;
      echo "Successful login!";
  
      echo "&lt;br /&gt; Welcome " .$_SESSION['username']. "!";
      echo "&lt;br /&gt;&lt;a href='signupform.php' &gt; SignUp &lt;/a&gt;";
      echo "&lt;br /&gt;&lt;a href='signinform.php' &gt; SignIn &lt;/a&gt;";
      echo "&lt;br /&gt;&lt;a href='logout.php' &gt; LogOut &lt;/a&gt;";
      }
      else{
      echo "Login failed!";
      echo "&lt;br /&gt;&lt;a href='signupform.php' &gt; SignUp &lt;/a&gt;";
      echo "&lt;br /&gt;&lt;a href='signinform.php' &gt; SignIn &lt;/a&gt;";
      }
  ?&gt;
  </code></pre>
  
  <p>On that page there is a link that allows you to log out (in case you have signed in) and by clicking that link you reach the following code:</p>
  
  <pre><code>&lt;?php
  include_once("db.php");
  ?&gt;
  &lt;?php
      session_start(); #starts the session        
  
      session_unset(); #removes all variables in the session
  
      session_destroy(); # destroys the session
  
      if(!$_SESSION['username'])
          echo "Succesfully logged out";  
  
      else
          echo "Error occurred";  
  
      ?&gt;
  </code></pre>
  
  <p>Then I get the following output when I run it on my localhost: Notice: Undefined index: username in C:\webserver\apache\htdocs\sign_in_up\logout.php on line 12</p>
  
  <p>Successfully logged out</p>