title: Clicking on submit in login form redirects to index.php and doesn't login
body: <p>I'm making my first dynamic website with html5/php/mysql.
  I don't get why this doesn't work.
  The index.php file at the root basically puts everything together.
  It's at example.com/ (or example.com/index.php):</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;?php
  include("layout/header.php");
  include("layout/layout.html");
  
  /*content BEGIN*/
  if (!empty($_SERVER['QUERY_STRING'])) {
      $page = $_SERVER['QUERY_STRING'];
      if(substr($page, -1) == '/') {
      $page = substr($page, 0, -1);
      }
      /*$page = basename($page);*/
      if (!file_exists("page/$page.php")){
          include("layout/404.html");
          }
      else{
          include("page/$page.php");
          }
  }
  else {
      include("page/home.php");
  }
  /*content END*/
  include("layout/footer.html");
  ?&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The .htaccess has this:</p>
  
  <pre><code>RewriteRule ^(.*)$ index.php?/$1 [L]
  </code></pre>
  
  <p>So basically example.com/foo/bar displays the content of example.com/page/foo/bar.php inside the site's layout.</p>
  
  <p>This post.php (located at example.com/page/admin/post.php and accessed with the layout at example.com/admin/post)
  works perfectly fine:</p>
  
  <pre><code>&lt;div class="text"&gt;
  &lt;?php
  session_start();
  if (!isset($_SESSION['login'])){
      header("Location: /admin/login");
      }
  if(isset($_POST['submit']) &amp;&amp; ($_POST['submit'])=="Post"){
      if (!isset($_POST["title"]) || (trim($_POST["title"])=="")){
          echo "Insert a title.";
          }
      elseif(!isset($_POST["post"]) || (trim($_POST["post"])=="")){
          echo "You forgot the post";
          }
      elseif(!isset($_POST["author"]) || (trim($_POST["author"])=="")){
          echo "Write your name";
          }
      else{
          $postTitle = $_POST["title"];
          $post = $_POST["post"];
          $postAuthor = $_POST["author"];
          include "mysql.php";
          $sql = new mysql();
          $sql-&gt;connect();
          $sql-&gt;insert("post",array($postTitle, $post, $postAuthor),"title,post,author");
          echo "Great Success!";
          $sql-&gt;disc();
          }
  }
  else{
  ?&gt;
  &lt;form action="&lt;?php $_SERVER["PHP_SELF"];?&gt;" method="post"&gt;
  &lt;h2&gt;New Post:&lt;/h2&gt;
  Title:
  &lt;br /&gt;
  &lt;input name="title" type="text" size="64" /&gt;
  &lt;br /&gt;
  Text:
  &lt;br /&gt;
  &lt;textarea name="post" cols="80" rows="20"&gt;&lt;/textarea&gt;
  &lt;br /&gt;
  Author:
  &lt;br /&gt;
  &lt;input name="author" type="text" size="32"  /&gt;
  &lt;br /&gt;
  &lt;input name="submit" type="submit" value="Post" /&gt;
  &lt;/form&gt;
  &lt;?php
  }
  ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>So as I said this works fine.
  But my login panel (example.com/page/admin/login.php OR example.com/admin/login) only works if I access it directly (example.com/page/admin/login.php), so without the layout around it.
  If I access it the "right way" through example.com/admin/login it loads but ALWAYS redirects me to example.com/index.php (this EXACT URL, not example.com/) upon clicking submit, and doesn't actually log me in.
  Here's the code for login.php:</p>
  
  <pre><code>&lt;div class="text"&gt;
  &lt;?php
  session_start();
  if(isset($_SESSION['login'])){
      header("Location: /admin/panel");
      }
  if(isset($_POST["submit"]) &amp;&amp; (trim($_POST["submit"])=="Login")){
      if (!isset($_POST["password"]) || (trim($_POST["password"])=="")){
          echo "Insert password";
          }
      elseif (!isset($_POST["username"]) || (trim($_POST["username"])=="")){
          echo "Insert your username";
          }
      $username = trim(filter_var($_POST['username'], FILTER_SANITIZE_STRING));
      $password = trim(filter_var($_POST['password'], FILTER_SANITIZE_STRING));
      $password = sha1($password);
      include "mysql.php";
      $sql = new mysql();
      $sql-&gt;connect();
      $loginlist = $sql-&gt;query("SELECT user_id FROM users WHERE username = '$username' AND password = '$password'");
      if(mysql_num_rows($loginlist)==0){
          echo "Username/password wrong";
          }
      else{
          $userdata = $sql-&gt;extract($loginlist);
          $_SESSION["login"] = $userdata-&gt;user_id;
          $sql-&gt;disc();
          header("Location: /admin/panel");
          }
      }
      else{
  ?&gt;
  &lt;h2&gt;Admin panel login&lt;/h2&gt;
  &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
  Username:
  &lt;br&gt;
  &lt;input name="username" type="text" size="16"&gt;
  &lt;br&gt;
  Password:
  &lt;br&gt;
  &lt;input name="password" type="password" size="64"&gt;
  &lt;br&gt;
  &lt;input name="submit" type="submit" value="Login"&gt;
  &lt;br&gt;
  &lt;/form&gt;
  &lt;?php 
  } 
  ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Also the apache log says:</p>
  
  <pre><code>"POST /index.php HTTP/1.1" 200 2906
  </code></pre>
  
  <p>For every time this happens. </p>