Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.     include("config.php");
  3.     session_start();
  4.  
  5.     $username = "";
  6.     $email = "";
  7.     $errors = array();
  8.  
  9.     if(isset($_POST['reg_user'])){
  10.         $username = mysqli_real_escape_string($conn, $_POST['username']);
  11.         $email = mysqli_real_escape_string($conn, $_POST['email']);
  12.         $password_1 = mysqli_real_escape_string($conn, $_POST['password']);
  13.  
  14.         if(empty($username)){ array_push($errors, "Username is required"); }
  15.         if(empty($email)){ array_push($errors, "Email is required"); }
  16.         if(empty($password_1)){ array_push($errors, "Password is required"); }
  17.  
  18.         $user_check_query = "SELECT * FROM users WHERE username = '$username' OR email='$email' LIMIT 1";
  19.  
  20.         $result = mysqli_query($conn, $user_check_query);
  21.  
  22.         $user = mysqli_fetch_assoc($result);
  23.  
  24.         if($user){
  25.             if($user['username'] === $username) {
  26.                 array_push($errors, "Username already exists");
  27.             }
  28.  
  29.             if($user['email'] === $email) {
  30.                 array_push($errors, "Email already exists");
  31.             }
  32.         }
  33.  
  34.         if (count($errors) === 0){
  35.             $password = md5($password_1); //Encrypts pass before saving.
  36.             echo $password ;
  37.  
  38.             $query = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
  39.  
  40.             mysqli_query($conn, $query);
  41.  
  42.             $_SESSION['username'] = $username;
  43.             $_SESSION['success'] = "You are now registered in.";
  44.  
  45.             header('location: login.php');
  46.         }
  47.     }
  48.  
  49.     if (isset($_POST['login_user'])){
  50.         $username = mysqli_real_escape_string($conn, $_POST['username']);
  51.         $password = mysqli_real_escape_string($conn, $_POST['password']);
  52.             if(empty($username)){
  53.                 array_push($errors, "Username is required");
  54.             }
  55.             if(empty($password)){
  56.                 array_push($errors, "Password is required");
  57.             }
  58.  
  59.             if(count($errors) == 0){
  60.                 $password = md5($password);
  61.                 $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  62.                 $results = mysqli_query($conn, $query);
  63.                     if(mysqli_num_rows($results) == 1){
  64.                         $_SESSION['username'] = $username;
  65.                         $_SESSION['success'] = "You are now logged in.";
  66.                         header('location: index.php');
  67.                     } else {
  68.                         array_push($errors, "Wrong username/password combination");
  69.                     }
  70.             }
  71. }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement