Advertisement
lowheartrate

can't login

Jun 5th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. // LOGIN.PHP
  2.  
  3.  
  4. <h1>Login</h1>
  5.  
  6. <form method="POST">
  7.     <input style="width: 200px;" type="text" name="username"><br />
  8.     <input style="width: 200px;" type="password" name="password"><br />
  9.     <input style="width: 200px; margin-top: 10px;" type="submit"><br />
  10.     <a href="register.php">Register</a>
  11. </form>
  12.  
  13. <?php
  14.     error_reporting(0);
  15.  
  16.     session_start();
  17.         if(isset($_POST['username'], $_POST['password'])) {
  18.             require 'core/connect.php';
  19.  
  20.             $query = dbConnect()->prepare("SELECT username, password FROM users WHERE username=:username AND password=:password");
  21.             $query->bindParam(':username', $_POST['username']);
  22.             $query->bindParam(':password', ($_POST['password']));
  23.             $query->execute();
  24.  
  25.             if($row = $query->fetch()) {
  26.                 $_SESSION['username'] = $row['username'];
  27.                 $_SESSION['password'] = $row['password'];
  28.                 header("Location: index.php");
  29.             } else {
  30.                 echo '<p style="color: red;">Invalid username/password</p>';
  31.             }
  32.         }
  33. ?>
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. // REGISTER.PHP
  46.  
  47.  
  48. <h1>Register</h1>
  49.  
  50. <form method="POST">
  51.     <input style="margin-bottom: 5px;" type="text" name="username" placeholder="Username" required><br />
  52.     <input style="margin-bottom: 5px;" type="password" name="password" placeholder="Password" required><br />
  53.     <input style="margin-bottom: 5px;" type="password" name="cpassword" placeholder="Confirm Password" required><br />
  54.     <input style="margin-bottom: 10px;" type="email" name="email" placeholder="Email Address" required><br />
  55.     <input style="width: 175px;" type="submit">
  56. </form>
  57.  
  58. <?php
  59. session_start();
  60.  
  61.     if(isset($_POST['username'], $_POST['password'], $_POST['cpassword'], $_POST['email'])){
  62.         require 'core/connect.php';
  63.  
  64.         error_reporting(0);
  65.  
  66.         $username = $_POST['username'];
  67.         $password = $_POST['password'];
  68.         $cpassword = $_POST['cpassword'];
  69.         $email = $_POST['email'];
  70.  
  71.         $query = dbConnect()->prepare("INSERT INTO users (username, password, cpassword, email) VALUES (:username, :password, :cpassword, :email)");
  72.         $query->bindParam(':username', $_POST['username']);
  73.         $query->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
  74.         $query->bindParam(':cpassword', password_hash($_POST['cpassword'], PASSWORD_BCRYPT));
  75.         $query->bindParam(':email', $_POST['email']);
  76.  
  77.         // Check to make sure username is no less then 6 characters,
  78.         // and is no more then 25 characters.
  79.         if(strlen($username)<6 || strlen($username)>25) {
  80.             echo '<p style="color: red;">Username must be between 6 and 25 characters</p>';
  81.  
  82.         } elseif($password != $cpassword) {
  83.             // If password fields don't match - show the following error
  84.             echo '<p style="color: red;">Passwords do not match</p>';
  85.  
  86.         } elseif(strlen($password)<8 || strlen($cpassword)<8) {
  87.             // If password field is less then 8 characters show this error:
  88.             echo '<p style="color: red;">Password must be at least 8 characters</p>';
  89.  
  90.         } else {
  91.             // If no errors are found, write credentials to database
  92.             $query->execute();
  93.             header("Location: index.php");
  94.         }
  95.     }
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement