title: PHP sessions logging in and logging out
body: <p>I have to make a simple site which you can log in and out of, and if the user is logged in they see some features which they otherwise would not. I'm not very good with web development however I have managed to get something together which seems to have worked. I've decided I don't want to redirect the user to another page when logging in and logging out so this has made it a bit harder for me to understand.</p>
  
  <p>I just wondered if I'm going about the session starts and destroy in the right way and if anyone could give me any pointers as to making it better if that's even possible.</p>
  
  <pre><code>&lt;?php
  
  
  if(isset($_POST['logout'])) {
      session_destroy();
      }
  }
      session_start();
  if(!isset($_SESSION['username'])) {
      if (!empty($_POST['username']) &amp;&amp; !empty($_POST['password'])) {
          $result = mysql_query("SELECT * FROM users WHERE username ='$_POST['username']' AND password = '$_POST['password']'");
          if(mysql_num_rows($result)) 
              $_SESSION['username'] = $_POST['username'];
          }
          else {
              echo "";
          }
      }
  }
  ?&gt;
          &lt;?php if(!isset($_SESSION['username'])) {
                  echo '&lt;div id = "account"&gt;
                          &lt;form name="input" action="index.php" method="post"&gt;
                              Username:&lt;input type="text" name="username" /&gt; Password:&lt;input type="password" name="password" /&gt;
                              &lt;input type="submit" value="GO!" /&gt;
                          &lt;/form&gt;
              }
              else {
                      echo "Signed in"
                      &lt;form name='logout' action='index.php' method='post'&gt;
                      &lt;input type='submit'value='Reset' name='logout'/&gt;
                      ";
              } ?&gt; 
              &lt;?php
              $test = mysql_query("SELECT * FROM posts ORDER BY post_id DESC");
              if($test) { 
                  while($row = mysql_fetch_array($test)) { 
                      echo '&lt;div class="posts"&gt;';
                          echo "$row[post]"; 
                      echo '&lt;/div&gt;';
                  }
              }
  </code></pre>