title: How get a login page to go back to the homepage in php
body: <p>I have login code where the user can log into a site and the details are picked up from a database. When the user logs in a want them to be taken back to the homepage of the site, I cant seem to get this to work.</p>
  
  <p>Here is my code</p>
  
  <p>main login</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
     
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
          &lt;title&gt;Buzz Party | Login&lt;/title&gt;
          &lt;meta name="description" content=""&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
          &lt;/head&gt;
  &lt;?php
  session_start();
  session_destroy();
  ?&gt;
  
  &lt;body&gt; 
       
  
      
  &lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"&gt;
  &lt;tr&gt;
  &lt;form name="form1" method="post" action="checklogin.php"&gt;
  &lt;td&gt;
  &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt;
  &lt;tr&gt;
  &lt;td colspan="3"&gt;&lt;strong&gt;Member Login &lt;/strong&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="78"&gt;Username&lt;/td&gt;
  &lt;td width="6"&gt;:&lt;/td&gt;
  &lt;td width="294"&gt;&lt;input name="myusername" type="text" id="myusername"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Password&lt;/td&gt;
  &lt;td&gt;:&lt;/td&gt;
  &lt;td&gt;&lt;input name="mypassword" type="text" id="mypassword"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;td&gt;&lt;input type="submit" name="Submit" value="Login"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/td&gt;
  &lt;/form&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>check login</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>?php
  
  $host="localhost"; // Host name 
  $username="root"; // Mysql username 
  $password="root"; // Mysql password 
  $db_name="login"; // Database name 
  $tbl_name="users"; // Table name 
  
  // Connect to server and select databse.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // username and password sent from form 
  $myusername=$_POST['myusername']; 
  $mypassword=$_POST['mypassword']; 
  
  // To protect MySQL injection (more detail about MySQL injection)
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
  $myusername = mysql_real_escape_string($myusername);
  $mypassword = mysql_real_escape_string($mypassword);
  $sql="SELECT * FROM $tbl_name WHERE name='$myusername' and password='$mypassword'";
  $result=mysql_query($sql);
  
  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){
  
  // Register $myusername, $mypassword and redirect to file "login_success.php"
  session_register("sername");
  session_register("password"); 
  header("location:login.php");
  }
  else {
  echo "Wrong Username or Password";
  }
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>login success</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  session_start();
  if isset($_SESSION['myusername'])
  {
  header("Location:myindex.php");
  }
  ?&gt;</code></pre>
  </div>
  </div>
  </p>