title: How to redirect after successful login? [PHP]
body: <p>I've created a login system. The registration page works like a charm. But there's some problem with the login page. When I try to login, it logs me in, but it stays on the same login page. I mean there is no visible change in the login page even after logging in. I want it to redirect after a successful login, I've tried headers but for some strange reason it didnt work. </p>
  
  <p>My code (PHP in the page):</p>
  
  <pre><code>&lt;?php  //Start the Session
  session_start();
   require('connect.php');
  //3. If the form is submitted or not.
  //3.1 If the form is submitted
  if (isset($_POST['username']) and isset($_POST['password'])){
  //3.1.1 Assigning posted values to variables.
  $username = $_POST['username'];
  $password = $_POST['password'];
  //3.1.2 Checking the values are existing in the database or not
  $query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
  
  $result = mysql_query($query) or die(mysql_error());
  $count = mysql_num_rows($result);
  //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
  if ($count == 1){
  $_SESSION['username'] = $username;
  header("location:redirectafterlogin.php"); //header to redirect, but doesnt work
  }else{
  //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
  echo "Invalid Login Credentials.";
  }
  }
  
  ?&gt;    
  </code></pre>
  
  <p>HTML in the page: </p>
  
  <pre><code>&lt;title&gt; Login&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="usr_style.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;!-- Form for logging in the users --&gt;
  
  &lt;div class="register-form"&gt;
  
  &lt;p&gt;Please login to continue&lt;/p&gt;
  
  &lt;h1&gt;Login&lt;/h1&gt;
  &lt;form action="login.php" method="POST"&gt;
      &lt;p&gt;&lt;label&gt;User Name : &lt;/label&gt;
      &lt;input id="username" type="text" name="username" required placeholder="username" /&gt;&lt;/p&gt;
  
       &lt;p&gt;&lt;label&gt;Password&amp;nbsp;&amp;nbsp; : &lt;/label&gt;
       &lt;input id="password" type="password" name="password" required placeholder="password" /&gt;&lt;/p&gt;
  
      &lt;a class="btn" href="register.php"&gt;Register&lt;/a&gt;
      &lt;input class="btn" type="submit" name="submit" value="Login" /&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>EDIT:</p>
  
  <p>Redirectafterlogin.php code (Its basically a guestbook):</p>
  
  <pre><code>&lt;body&gt;
  &lt;div class="main"&gt;
  &lt;br&gt;
  
  
          &lt;div id="title"&gt;&lt;h2&gt;Post&lt;/h2&gt;&lt;/div&gt;
          &lt;br&gt;
      &lt;div id="f"&gt;
      &lt;form id="form1" name="form1" method="post" action="addpost.php"&gt;
  
      &lt;table id="g"&gt;
  
          &lt;td&gt;Post&lt;/td&gt;
          &lt;td&gt;&lt;textarea name="comment" placeholder="Please type your post here...." rows=10 cols=50 id="comment"&gt;&lt;/textarea&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&lt;input id="btn" type="submit" name="Submit" value="Post" /&gt; &lt;input id="btn" type="reset" name="Submit2" value="Reset" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;/table&gt;
  
      &lt;/form&gt;
  
  &lt;/div&gt;
  &lt;hr&gt;
  
  
  
  &lt;?php
  
  $host=""; // Host name
  $username=""; // Mysql username
  $password=""; // Mysql password
  $db_name=""; // Database name
  $tbl_name=""; // Table name
  
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
  mysql_select_db("$db_name")or die("cannot select DB");
  
  $sql="SELECT * FROM $tbl_name ORDER BY ID DESC LIMIT 50";
  $result=mysql_query($sql);
  
  while($rows=mysql_fetch_array($result)){
  ?&gt;
  
  
  &lt;div id="post_info"&gt;
  &lt;br&gt;
  
  
  &lt;div id="author"&gt;
  Name:
  &lt;?php echo htmlspecialchars($rows['name']); ?&gt; 
  &lt;/div&gt;
  
  &lt;div id="time"&gt;
  Time: &lt;?php echo htmlspecialchars($rows['datetime']); ?&gt;
  &lt;/div&gt;
  
  &lt;br&gt;
  
  &lt;div id="post"&gt;
  &lt;pre&gt;&lt;?php echo htmlspecialchars($rows['comment']); ?&gt;&lt;/pre&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;br&gt;
  
  &lt;?php
  }
  mysql_close(); //close database
  ?&gt;
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 6 1 Warning: Cannot modify header information - headers already sent by (output started at /customers/7/e/5/sattapesatta.com/httpd.www/login.php:5) in /customers/7/e/5/sattapesatta.com/httpd.www/login.php on line 20</p>
  </blockquote>