title: Login / Registration PHP form
body: <p>having a bit of trouble with my login / reg forms</p>
  
  <p>Basically when i register (create new user) it takes me to the login.php script and not the register script. </p>
  
  <p>The login form is in the "header.php" page so its at the top of every page including the register form. But dont think that would be an issue?</p>
  
  <p>Register form </p>
  
  <pre><code>&lt;?php 
  include("config.php");
  include("header.php");
  ?&gt;
  
  &lt;div id="contentwrap"&gt;
  
  &lt;form name="myuserform" method="POST" action="register.php" onsubmit="return validateForm();"&gt;
  &lt;tr class='alt'&gt;
  &lt;td&gt;email address: &lt;td&gt;&lt;input type="text" name="email"&gt; 
  &lt;tr class='alt'&gt;
  &lt;td&gt;Password: &lt;td&gt;&lt;input type="password" name="password"&gt;
  &lt;tr class='alt'&gt;
  &lt;td&gt;Your name: &lt;td&gt;&lt;input type="text" name="username"&gt;
  &lt;tr class='alt'&gt;
  &lt;td&gt;&lt;input type="submit" name="adduser" value="Sign me up!"&gt; 
  &lt;/form&gt; 
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Register.php</p>
  
  <pre><code>&lt;?php 
  
  
  if (isset($_POST['adduser']))
  {
      $error = "";
  
      $username = mysqli_real_escape_string($connection, $_POST['username']);
      $password = mysqli_real_escape_string($connection, $_POST['password']);
      $md5_pass = md5($password);
      $email = mysqli_real_escape_string($connection, $_POST['email']);
  
      if (!isset($username) || empty($username) ||
          !isset($password) || empty($password) ||
          !isset($email) || empty($email))
      {
          $error = "All fields must be filled out";
      }
      else if (user_exists($connection, $username))
      {
          $error = "Username already registered";
      }
      else if (strlen($password) &lt; 6)
      {
          $error = "Password must be at least 6 characters";
      }
      else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // check if email looks valid
      {
          $error = "Please enter a valid email";
      }
  
      if ($error == "")
      {
          //$query = "INSERT INTO users (email, password, username) VALUES ('{$email}','{$md5_pass}','{$username}')";
          $query = "INSERT INTO users (username, password, email) VALUES ('{$username}','{$md5_pass}','{$email}')";
          $result = mysqli_query($connection, $query);
  
          if ($result)
              echo " &lt;b&gt;Registered successfully!&lt;/b&gt;&lt;br/&gt;Please return to the &lt;a href='index.php'&gt;index&lt;/a&gt; to login.";
  
          else
              $error = "Unable to create new user";
      }
  
      if ($error != "") // redo error string check since the last block may have set it
      {
          echo "Error: {$error}. Please return to the previous page.";
      }
  
      exit();
  }
  ?&gt;
  </code></pre>
  
  <p>Login.php </p>
  
  <pre><code>&lt;?php
  include("config.php");
  
  if (isset($_POST['username']) &amp;&amp; !empty($_POST['username']) &amp;&amp;
      isset($_POST['password']) &amp;&amp; !empty($_POST['password']))
  {
      $username = mysqli_real_escape_string($connection, $_POST['username']);
      $password = md5($_POST['password']);
  
      $query = "SELECT * FROM users WHERE username='{$username}' AND password='{$password}'";
      $res = mysqli_query($connection, $query);
      if (mysqli_num_rows($res) &gt;= 1)
      {
          $row = mysqli_fetch_array($res);
          if($row['rank'] == "banned")
          {
              echo "You have been banned from the site.";
              exit();
          }
          $_SESSION['uid'] = $row['userid'];
          $_SESSION['username'] = $row['username'];
          if($row['rank'] == "admin")
          $_SESSION['is_admin'] = true;
  
          header("Location: index.php");
          exit();
      }
      else
      {
          echo "Username/password invalid. Return to the &lt;a href='index.php'&gt; home &lt;/a&gt;page";
          exit();
      }
  }
  
  echo "Something went wrong, try again"; &lt;--- this is the result im getting
  ?&gt;
  </code></pre>
  
  <p>here is the login form (apart of header.php)</p>
  
  <pre><code>    &lt;?php
  if (!isset($_SESSION['uid']) || empty($_SESSION['uid']))
  {
      echo "&lt;form action='login.php' method='post'&gt;
      Username: &lt;input type='text' name='username' Placeholder='Username' style='width:100px;'/&gt;&amp;nbsp;
      Password: &lt;input type='password' name='password' Placeholder='Password' style='width:100px;' /&gt;&amp;nbsp;
      &lt;input type='submit' name='submit' value='Log In' /&gt;";
      echo "&lt;div id='freeman'&gt;
      &lt;a href='signup.php'&gt; &lt;img src='images/register.jpg' width='60px' height='60px' /&gt; &lt;/a&gt;
      &lt;/div&gt;";
  } else {
  echo "You are logged is as {$_SESSION['username']} &amp;bull; &lt;a href='logout.php'&gt;Logout&lt;/a&gt;";
  }
  ?&gt;
  </code></pre>