SHARE
TWEET

functions.php

a guest Dec 11th, 2019 270 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. session_start();
  3. // connect to database
  4. $db = mysqli_connect('localhost', 'root', '', 'multi_login');
  5.  
  6. // variable declaration
  7. $username = "";
  8. $email    = "";
  9. $errors   = array();
  10.  
  11. // call the register() function if register_btn is clicked
  12. if (isset($_POST['register_btn'])) {
  13.     register();
  14. }
  15.  
  16. // REGISTER USER
  17. function register(){
  18.     // call these variables with the global keyword to make them available in function
  19.     global $db, $errors, $username, $email;
  20.  
  21.     // receive all input values from the form. Call the e() function
  22.     // defined below to escape form values
  23.     $username    =  e($_POST['username']);
  24.     $email       =  e($_POST['email']);
  25.     $password_1  =  e($_POST['password_1']);
  26.     $password_2  =  e($_POST['password_2']);
  27.  
  28.     // form validation: ensure that the form is correctly filled
  29.     if (empty($username)) {
  30.         array_push($errors, "Username is required");
  31.     }
  32.     if (empty($email)) {
  33.         array_push($errors, "Email is required");
  34.     }
  35.     if (empty($password_1)) {
  36.         array_push($errors, "Password is required");
  37.     }
  38.     if ($password_1 != $password_2) {
  39.         array_push($errors, "The two passwords do not match");
  40.     }
  41.  
  42.     // register user if there are no errors in the form
  43.     if (count($errors) == 0) {
  44.         $password = md5($password_1);//encrypt the password before saving in the database
  45.  
  46.         if (isset($_POST['user_type'])) {
  47.             $user_type = e($_POST['user_type']);
  48.             $query = "INSERT INTO users (username, email, user_type, password)
  49.                       VALUES('$username', '$email', '$user_type', '$password')";
  50.             mysqli_query($db, $query);
  51.             $_SESSION['success']  = "New user successfully created!!";
  52.             header('location: index.php');
  53.         }else{
  54.             $query = "INSERT INTO users (username, email, user_type, password)
  55.                       VALUES('$username', '$email', 'user', '$password')";
  56.             mysqli_query($db, $query);
  57.  
  58.             // get id of the created user
  59.             $logged_in_user_id = mysqli_insert_id($db);
  60.  
  61.             $_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
  62.             $_SESSION['success']  = "You are now logged in";
  63.             header('location: index.php');             
  64.         }
  65.     }
  66. }
  67.  
  68. // return user array from their id
  69. function getUserById($id){
  70.     global $db;
  71.     $query = "SELECT * FROM users WHERE id=" . $id;
  72.     $result = mysqli_query($db, $query);
  73.  
  74.     $user = mysqli_fetch_assoc($result);
  75.     return $user;
  76. }
  77.  
  78. // escape string
  79. function e($val){
  80.     global $db;
  81.     return mysqli_real_escape_string($db, trim($val));
  82. }
  83.  
  84. function display_error() {
  85.     global $errors;
  86.  
  87.     if (count($errors) > 0){
  88.         echo '<div class="error">';
  89.             foreach ($errors as $error){
  90.                 echo $error .'<br>';
  91.             }
  92.         echo '</div>';
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top