Guest User

Untitled

a guest
Sep 13th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.21 KB | None | 0 0
  1. <?php
  2. //Declaring variables to prevent errors
  3. $firstname = ""; //First name
  4. $lastname = ""; //Last name
  5. $email= ""; //email
  6. $password = ""; //password
  7. $confirmpassword= ""; //confirm password
  8. $username= ""; //username
  9. $address= ""; //address
  10. $mobile= "";//mobile
  11. $licence_key= "";
  12. $specialist="";
  13.  
  14.  
  15. $error_array = array(); //Holds error messages
  16.  
  17. if(isset($_POST['register_button'])){
  18.  
  19.     //Registration form values
  20.  
  21.     //First name
  22.     $firstname = strip_tags($_POST['firstname']); //Remove html tags
  23.     $firstname = str_replace(' ', '', $firstname); //remove spaces
  24.     $firstname = ucfirst(strtolower($firstname)); //Uppercase first letter
  25.     $_SESSION['firstname'] = $firstname; //Stores first name into session variable
  26.  
  27.     //Last name
  28.     $lastname = strip_tags($_POST['lastname']); //Remove html tags
  29.     $lastname = str_replace(' ', '', $lastname); //remove spaces
  30.     $lastname = ucfirst(strtolower($lastname)); //Uppercase first letter
  31.     $_SESSION['lastname'] = $lastname; //Stores last name into session variable
  32.  
  33.     //email
  34.     $email = strip_tags($_POST['email']); //Remove html tags
  35.     $email = str_replace(' ', '', $email); //remove spaces
  36.     $email = ucfirst(strtolower($email)); //Uppercase first letter
  37.     $_SESSION['email'] = $email; //Stores email into session variable
  38.  
  39.  
  40.     //Password
  41.     $password = strip_tags($_POST['password']); //Remove html tags
  42.     $confirmpassword= strip_tags($_POST['confirmpassword']); //Remove html tags
  43.  
  44.     //Address
  45.     $address = strip_tags($_POST['address']); //Remove html tags
  46.     $address = str_replace(' ', '', $address); //remove spaces
  47.     $address = ucfirst(strtolower($address)); //Uppercase first letter
  48.     $_SESSION['address'] = $address; //Stores address into session variable
  49.  
  50.     //Mobile
  51.     $mobile = strip_tags($_POST['mobile']); //Remove html tags
  52.     $mobile = str_replace(' ', '', $mobile); //remove spaces
  53.     $_SESSION['mobile'] = $mobile; //Stores first name into session variable
  54.  
  55.  
  56.     //licence key
  57.     $licence_key = strip_tags($_POST['licence_key']); //Remove html tags
  58.     $licence_key = str_replace(' ', '', $licence_key); //remove spaces
  59.     $_SESSION['licence_key'] = $licence_key; //Stores first name into session variable
  60.  
  61.  
  62.     //Specilaist
  63.     $specialist = strip_tags($_POST['specialist']); //Remove html tags
  64.     $specialist = str_replace(' ', '', $specialist); //remove spaces
  65.     $specialist = ucfirst(strtolower($specialist)); //Uppercase first letter
  66.     $_SESSION['specialist'] = $specialist; //Stores first name into session variable
  67.  
  68.  
  69.     //if($em == $em2) {
  70.         //Check if email is in valid format
  71.         //if(filter_var($em, FILTER_VALIDATE_EMAIL)) {
  72.  
  73.             //$em = filter_var($em, FILTER_VALIDATE_EMAIL);
  74.  
  75.             //Check if email already exists
  76.             $email_check = mysqli_query($con, "SELECT email FROM users WHERE email='$email'");
  77.  
  78.             //Count the number of rows returned
  79.             $num_rows = mysqli_num_rows($email_check);
  80.  
  81.             if($num_rows > 0) {
  82.                 array_push($error_array, "Email already in use<br>");
  83.             }
  84.  
  85.         }
  86.         else {
  87.             array_push($error_array, "Invalid email format<br>");
  88.         }
  89.  
  90.  
  91.     }
  92.     //else {
  93.         //array_push($error_array, "Emails don't match<br>");
  94.     }
  95.  
  96.  
  97.     if(strlen($firstname) > 25 || strlen($firstname) < 2) {
  98.         array_push($error_array, "Your first name must be between 2 and 25 characters<br>");
  99.     }
  100.  
  101.     if(strlen($lastname) > 25 || strlen($lastname) < 2) {
  102.         array_push($error_array,  "Your last name must be between 2 and 25 characters<br>");
  103.     }
  104.  
  105.     if($password != $confirmpassword) {
  106.         array_push($error_array,  "Your passwords do not match<br>");
  107.     }
  108.     else {
  109.         if(preg_match('/[^A-Za-z0-9]/', $password)) {
  110.             array_push($error_array, "Your password can only contain english characters or numbers<br>");
  111.         }
  112.     }
  113.  
  114.     if(strlen($password > 30 || strlen($password) < 5)) {
  115.         array_push($error_array, "Your password must be betwen 5 and 30 characters<br>");
  116.     }
  117.  
  118.  
  119.     if(empty($error_array)) {
  120.         $password = md5($password); //Encrypt password before sending to database
  121.  
  122.         //Generate username by concatenating first name and last name
  123.         $username = strtolower($fname . "_" . $lname);
  124.         $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");
  125.  
  126.  
  127.         $i = 0;
  128.         //if username exists add number to username
  129.         while(mysqli_num_rows($check_username_query) != 0) {
  130.             $i++; //Add 1 to i
  131.             $username = $username . "_" . $i;
  132.             $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");
  133.         }
  134.  
  135.         //Profile picture assignment
  136.         //$rand = rand(1, 2); //Random number between 1 and 2
  137.  
  138.         //if($rand == 1)
  139.         //  $profile_pic = "assets/images/profile_pics/defaults/head_deep_blue.png";
  140.         //else if($rand == 2)
  141.             //$profile_pic = "assets/images/profile_pics/defaults/head_emerald.png";
  142.  
  143.  
  144.         $query = mysqli_query($con, "INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$password',  '0', '0', 'no', ',')");
  145.  
  146.         array_push($error_array, "<span style='color: #14C800;'>You're all set! Goahead and login!</span><br>");
  147.  
  148.         //Clear session variables
  149.         $_SESSION['firstname'] = "";
  150.         $_SESSION['lastname'] = "";
  151.         $_SESSION['email'] = "";
  152.         $_SESSION['username'] = "";
  153.         $_SESSION['address'] = "";
  154.         $_SESSION['mobile'] = "";
  155.         $_SESSION['licence_key'] = "";
  156.         $_SESSION['specialist'] = "";
  157.        
  158.     }
  159.  
  160. }
  161. ?>
Add Comment
Please, Sign In to add comment