Advertisement
Guest User

server.php

a guest
May 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 2.00 KB | None | 0 0
  1. <?php
  2. session_start();
  3. // initializing variables
  4. $username = "";
  5. $email    = "";
  6. $errors = array();
  7. // connect to the database
  8. $db = mysqli_connect('zqjbeufeft', 'J9cbx2f6mQ', '', 'zqjbeufeft');
  9. // REGISTER USER
  10. if (isset($_POST['reg_user'])) {
  11.   // receive all input values from the form
  12.   $username = mysqli_real_escape_string($db, $_POST['username']);
  13.   $email = mysqli_real_escape_string($db, $_POST['email']);
  14.   $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  15.   $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  16.   // form validation: ensure that the form is correctly filled ...
  17.   // by adding (array_push()) corresponding error unto $errors array
  18.   if (empty($username)) { array_push($errors, "Username is required"); }
  19.   if (empty($email)) { array_push($errors, "Email is required"); }
  20.   if (empty($password_1)) { array_push($errors, "Password is required"); }
  21.   if ($password_1 != $password_2) {
  22.     array_push($errors, "The two passwords do not match");
  23.   }
  24.   // first check the database to make sure
  25.   // a user does not already exist with the same username and/or email
  26.   $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  27.   $result = mysqli_query($db, $user_check_query);
  28.   $user = mysqli_fetch_assoc($result);
  29.  
  30.   if ($user) { // if user exists
  31.     if ($user['username'] === $username) {
  32.       array_push($errors, "Username already exists");
  33.     }
  34.     if ($user['email'] === $email) {
  35.       array_push($errors, "email already exists");
  36.     }
  37.   }
  38.   // Finally, register user if there are no errors in the form
  39.   if (count($errors) == 0) {
  40.     $password = md5($password_1);//encrypt the password before saving in the database
  41.     $query = "INSERT INTO users (username, email, password)
  42.               VALUES('$username', '$email', '$password')";
  43.     mysqli_query($db, $query);
  44.     $_SESSION['username'] = $username;
  45.     $_SESSION['success'] = "You are now logged in";
  46.     header('location: index.php');
  47.   }
  48. }
  49. // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement