Advertisement
DavidWel

server.php

Jan 21st, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.  
  4.     // variable declaration
  5.     $username = "";
  6.     $email    = "";
  7.     $errors = array();
  8.     $_SESSION['success'] = "";
  9.  
  10.     // connect to database
  11.     $db = mysqli_connect('localhost', 'root', '', 'registration');
  12.  
  13.     // REGISTER USER
  14.     if (isset($_POST['reg_user'])) {
  15.         // receive all input values from the form
  16.         $username = mysqli_real_escape_string($db, $_POST['username']);
  17.         $email = mysqli_real_escape_string($db, $_POST['email']);
  18.         $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  19.         $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  20.  
  21.         // form validation: ensure that the form is correctly filled
  22.         if (empty($username)) {
  23.         //array_push($errors, "Username is required");
  24.         print("POST['username'] IS empty!");
  25.         }
  26.         if (empty($email)) {
  27.         //array_push($errors, "Email is required");
  28.         print("POST['email'] IS empty!");
  29.         }
  30.         if (empty($password_1)) {
  31.         //array_push($errors, "Password is required");
  32.         print("POST['pass'] IS empty!");
  33.         }
  34.  
  35.         if ($password_1 != $password_2) {
  36.             array_push($errors, "The two passwords do not match");
  37.         }
  38.  
  39.         // register user if there are no errors in the form
  40.         if (count($errors) == 0) {
  41.             $password = md5($password_1);//encrypt the password before saving in the database
  42.             $query = "INSERT INTO users (username, email, password)
  43.                       VALUES('$username', '$email', '$password')";
  44.             mysqli_query($db, $query);
  45.  
  46.             $_SESSION['username'] = $username;
  47.             $_SESSION['success'] = "You are now logged in";
  48.             header('location: index.php');
  49.         }
  50.  
  51.     }
  52.  
  53.     // ...
  54.  
  55.     // LOGIN USER
  56.     if (isset($_POST['login_user'])) {
  57.         $username = mysqli_real_escape_string($db, $_POST['username']);
  58.         $password = mysqli_real_escape_string($db, $_POST['password']);
  59.  
  60.         if (empty($username)) {
  61.             array_push($errors, "Username is required");
  62.         }
  63.         if (empty($password)) {
  64.             array_push($errors, "Password is required");
  65.         }
  66.  
  67.         if (count($errors) == 0) {
  68.             $password = md5($password);
  69.             $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  70.             $results = mysqli_query($db, $query);
  71.  
  72.             if (mysqli_num_rows($results) == 1) {
  73.                 $_SESSION['username'] = $username;
  74.                 $_SESSION['success'] = "You are now logged in";
  75.                 header('location: index.php');
  76.             }else {
  77.                 array_push($errors, "Wrong username/password combination");
  78.             }
  79.         }
  80.     }
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement