title: php sessions username/password
body: <p>I'm only just starting to learn PHP and have done ok so far but I have confused myself with sessions. </p>
  
  <p>If someone could have a look at my code and let me know where i have went wrong and how i can fix it that would be fantastic!</p>
  
  <p>The username/password is hard coded to admin 123 as instructed. </p>
  
  <p>Thanks</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;!DOCTYPE&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;if statements&lt;/title&gt;
  &lt;/head&gt;
  
  PHP
  
  &lt;body&gt;
  Login
  &lt;form action="username.php" method="post"&gt;
    &lt;p&gt;
      &lt;label for="username"&gt;Enter your username&lt;/label&gt;
      &lt;input type="text" name="username" id="username" /&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;label for="password"&gt;Enter your password&lt;/label&gt;
      &lt;input type="text" name="password" id="password" /&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;label for="GO"&gt;&lt;/label&gt;
      &lt;input type="submit" name="GO" id="GO" value="Submit" /&gt;
    &lt;/p&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;?php
  session_start();
  If($username==”admin” &amp;&amp; $password==”123”)
  
      $username = isset($_POST['username']) ;
      $password = isset($_POST['password']) ;
  
      if ($name != 'admin' || $pass != '123')
      {
          elseif ($username === '')
              die("ERROR: Please enter the username!");
  
          elseif ($password === '')
              die("ERROR: Please enter the password!");
  
          elseif ($name == "test" &amp;&amp; $pass == "test") {
  
              // authentication successful, save username in session:
              $_SESSION['user'] = 'admin';
  
              // redirect to index.php to welcome the logged in user:
              header('Location: index.html');
              exit;
          }
          else {
              die("ERROR: Incorrect username or password!");
          }
      }
  
  
      ?&gt;
  
  
  
  
  
  
  
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>EDIT</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;?php
  session_start();
  If($username==”admin” &amp;&amp; $password=="123”)
  
      $username = isset($_POST['username']) ;
      $password = isset($_POST['password']) ;
  
  
  
       if ($username == "admin" &amp;&amp; $password == "123") {
  
              // authentication successful, save username in session:
              $_SESSION['username'] = 'admin';
              $_SESSION['password'] = '123';
  
              // redirect to welcome.html to welcome the logged in user:
              header('Location: welcome.html');
              exit;
          }
          else {
              die("ERROR: Incorrect username or password!");
          }
  
  
      // no submitted data, display form:
      ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>EDIT AGAIN:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
  &lt;?php
  
  
  session_start();
  if(isset($_POST['username'], $_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];
      if($username === 'admin' &amp;&amp; $password === '123') 
      $_SESSION['username'] = 'admin';
      $_SESSION['password'] = '123';
  
      session_write_close();{
                    // redirect to welcome.html to welcome the logged in user:
              header('Location: welcome.html');
              exit;
      }
  }
  
  
          else {
              die("ERROR: Incorrect username or password!");
          }
  
  
      // no submitted data, display form:
      ?&gt;
  
  
  
  
  
  
  
  
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It kind of works now, but even with wrong username/password it goes to welcome.html</p>