title: PHP + Session doesn't login at first time
body: <p>My login system only logs in if I do it 2 times. In the first time, it doesn't log in (automatically).
  I have a simple form with username and password, with <code>action="login.php"</code>, which as you might think has the whole code for verify login, etc..and then, in the end, if everything is ok:</p>
  
  <pre><code>Cookie::set('page-main-login-cookie', serialize($arr), time()+60);
  header('location: ../index.php');
  </code></pre>
  
  <p>In the <code>index.php</code> (which contains the form that calls <code>login.php</code>), in the top of the page:</p>
  
  <pre><code>ob_start(); session_start(); include('cookies.php'); include('sessions.php'); 
  </code></pre>
  
  <p>After the <code>&lt;/html&gt;</code> tag:</p>
  
  <pre><code>if(Cookie::Exists('page-main-login-cookie')){
      $data = unserialize(stripslashes($_COOKIE['page-main-login-cookie']));
      if($data['status'] == 1){   
          Cookie::set('email', $data['email'], time() + (86400 * 7));
          Session::set('email', $data['email']);
          echo "&lt;script type='text/javascript'&gt; page_redirect(); &lt;/script&gt;";
      }else{
          echo "&lt;script type='text/javascript'&gt; page_error(); &lt;/script&gt;";
      }
      Cookie::Delete('page-main-login-cookie', time() - 3600);
  }
  </code></pre>
  
  <p>And while I'm writing this topic I wonder if the problem might be in the javascript. The code for <code>page_redirect()</code> is the following:</p>
  
  <pre><code>function page_redirect(){
      alert("Done!");
      setTimeout(function(){
          location.reload();
      }, 1500);
  }
  </code></pre>
  
  <p>What have I tried so far?</p>
  
  <ol>
  <li>Changed <code>header('location: ../index.php');</code> to <code>header('location: http://www.mysite.com');</code></li>
  <li>Added <code>session_write_close();</code> after <code>Session::set('email', $data['email']);</code></li>
  </ol>
  
  <p>None of them worked.</p>