title: How to create a very simple username - password login in PHP?
body: <p>index.php</p>
  
  <pre><code>&lt;?php
  if( $_SESSION['auth'] != 1 ) {
      require( 'login.php' );
  }
  else {
      echo "hello";
  }
  ?&gt;
  </code></pre>
  
  <p>login.php</p>
  
  <pre><code>&lt;?php
  $name = $_POST['name'];
  $pass = $_POST['pass'];
  
  if( isset($name) || isset($pass) )
  {
      if( empty($name) ) {
          die ("ERROR: Please enter username!");
      }
      if( empty($pass) ) {
          die ("ERROR: Please enter password!");
      }
  
  
      if( $name == "&lt;some name&gt;" &amp;&amp; $pass == "&lt;some password&gt;" )
      {
          // Authentication successful - Set session
          session_start();
          $_SESSION['auth'] = 1;
          setcookie("username", $_POST['name'], time()+(84600*30));
          echo "Access granted!";
      }
      else {
          echo "ERROR: Incorrect username or password!";
      }
  }
  
  
  // If no submission, display login form
  else {
  ?&gt;
      &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
      &lt;center&gt;
      &lt;form method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
      Username: &lt;input type="text" name="name" value="&lt;?php echo $_COOKIE['username']; ?&gt;"&gt;
      &lt;p /&gt;
      Password: &lt;input type="password" name="pass"&gt;
      &lt;p /&gt;
      &lt;input type="submit" name="submit" value="Log In"&gt;
      &lt;/center&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>So, as I'm still learning PHP, there's a few things I'm trying to figure out now:</p>
  
  <ul>
  <li>How do I get it so I can reload index.php and it displays 'hello'?</li>
  <li>How can I get login.php to auto-load index.php on a successful authentication so I can get it to that "hello"?</li>
  <li>Later, would using a cookie to store the user's submitted login data (so they don't have to refill the form to restore their session) have any potential problems?</li>
  </ul>
  
  <p>Help appreciated.</p>