title: This webpage has a redirect loop - PHP Login
body: <p>I'm trying out a login page example in php. I get the error: <strong>This webpage has a redirect loop</strong>
  Details say: Error code: ERR_TOO_MANY_REDIRECTS</p>
  
  <p>Here's my code:</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  include('login.php'); // Includes Login Script
  
  if(isset($_SESSION['login_user'])){
  header("location: profile.php");
  }
  ?&gt;
  
  
                  &lt;form action="" method="post"&gt;
                  &lt;label&gt;UserName :&lt;/label&gt;
                  &lt;input id="name" name="username" placeholder="username" type="text"&gt;
                  &lt;label&gt;Password :&lt;/label&gt;
                  &lt;input id="password" name="password" placeholder="**********" type="password"&gt;
                  &lt;input name="submit" type="submit" value=" Login "&gt;
                  &lt;span&gt;&lt;?php echo $error; ?&gt;&lt;/span&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>&lt;?php
  session_start(); 
  $error=''; 
  
  if (isset($_POST['submit'])) {
      if (empty($_POST['username']) || empty($_POST['password'])) {
          $error = "Username or Password is invalid";
      }
      else
      {
  
          $username=$_POST['username'];
          $password=$_POST['password'];
  
          $connection = mysql_connect("localhost", "root", "");
  
          $username = stripslashes($username);
          $password = stripslashes($password);
          $username = mysql_real_escape_string($username);
          $password = mysql_real_escape_string($password);
  
          $db = mysql_select_db("rjtest", $connection);
  
          $query = mysql_query("select * from login where myPassword='$password' AND myUserName='$username'", $connection);
          $rows = mysql_num_rows($query);
          if ($rows == 1) {
              $_SESSION['login_user']=$username;
              header("location: profile.php"); 
          } else {
              $error = "Username or Password is invalid";
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>profile.php</strong></p>
  
  <pre><code>&lt;?php
  include('session.php');
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Your Home Page&lt;/title&gt;
  &lt;link href="style.css" rel="stylesheet" type="text/css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="profile"&gt;
  &lt;b id="welcome"&gt;Welcome : &lt;i&gt;&lt;?php echo $login_session; ?&gt;&lt;/i&gt;&lt;/b&gt;
  &lt;b id="logout"&gt;&lt;a href="logout.php"&gt;Log Out&lt;/a&gt;&lt;/b&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>session.php</strong></p>
  
  <pre><code>&lt;?php
  
  $connection = mysql_connect("localhost", "root", "");
  
  $db = mysql_select_db("rjtest", $connection);
  session_start();
  
  $user_check=$_SESSION['login_user'];
  
  $ses_sql=mysql_query("select myUsername from login where myUsername='$user_check'", $connection);
  $row = mysql_fetch_assoc($ses_sql);
  $login_session =$row['username'];
  if(!isset($login_session)){
  
  header('Location: index.php'); 
  }
  ?&gt;
  </code></pre>
  
  <p>And <strong>logout.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  if(session_destroy())
  {
  header("Location: index.php");
  }
  ?&gt;
  </code></pre>
  
  <p>I can't seem figure out why. The site where I got this code is now inactive, so that's why Im asking this here. Hope you guys could help me out. Sorry for the long post though.</p>