daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Mar 17th, 2018 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     session_start();
  3.     include("includes/db.php");
  4.  
  5.     //Declaring variables to prevent errors
  6.  
  7.     $fname = ""; //Holds First Name
  8.     $lname = ""; //Holds Last Name
  9.     $em = ""; //Holds email address
  10.     $em2 = ""; //Holds confirm email address
  11.     $password  = ""; //Holds password
  12.     $password2  = ""; //Holds Confirm password
  13.     $error_array = array(); //Holds error messages
  14.  
  15.     if(isset($_POST['register_button'])){
  16.         //Registration form values
  17.  
  18.         //First Name
  19.  
  20.         $fname = strip_tags($_POST['reg_fname']); //Remove HTML Tags
  21.         $fname = str_replace(' ','', $fname); //Removes spaces
  22.         $fname = ucfirst(strtolower($fname)); //Uppercase only first letter
  23.         $_SESSION['reg_fname'] = $fname; //Stores first name in Session variable
  24.  
  25.         //Last Name
  26.  
  27.         $lname = strip_tags($_POST['reg_lname']); //Remove HTML Tags
  28.         $lname = str_replace(' ','', $lname); //Removes spaces
  29.         $lname = ucfirst(strtolower($lname)); //Uppercase only first letter
  30.         $_SESSION['reg_lname'] = $lname; //Stores last name in Session variable
  31.  
  32.         //Email
  33.  
  34.         $em = strip_tags($_POST['reg_email']); //Remove HTML Tags
  35.         $em = str_replace(' ','', $em); //Removes spaces
  36.         $em = (strtolower($em)); //Uppercase only first letter
  37.         $_SESSION['reg_email'] = $em; //Stores email address in Session variable
  38.  
  39.         //Confirm Email
  40.  
  41.         $em2 = strip_tags($_POST['reg_email2']); //Remove HTML Tags
  42.         $em2 = str_replace(' ','', $em2); //Removes spaces
  43.         $em2 = (strtolower($em2)); //Uppercase only first letter
  44.         $_SESSION['reg_email2'] = $em2; //Stores confirm email address in Session variable
  45.  
  46.  
  47.         //Password
  48.  
  49.         $password = strip_tags($_POST['reg_password']); //Remove HTML Tags
  50.         $password2 = strip_tags($_POST['reg_password2']); //Remove HTML Tags.
  51.  
  52.         //Current Date
  53.         $date = date("Y-m-d");
  54.  
  55.         if($em == $em2){
  56.             //Check if email is in valid format
  57.             if(filter_var($em, FILTER_VALIDATE_EMAIL)){
  58.                 $em = filter_var($em, FILTER_VALIDATE_EMAIL);
  59.  
  60.                 //Check if email already exists
  61.                 $e_check = mysqli_query($con, "SELECT email FROM users WHERE email = '$em' ");
  62.  
  63.                 //Count number of rows returned
  64.                 $num_rows = mysqli_num_rows($e_check);
  65.  
  66.                 if($num_rows>0){
  67.                     array_push($error_array,"Email already in use<br>");
  68.                 }
  69.             }
  70.             else{
  71.                 array_push($error_array,"Invalid email format<br>");
  72.             }
  73.         }
  74.         else{
  75.             array_push($error_array,"Emails don't match<br>");
  76.         }
  77.  
  78.         //Field validation
  79.  
  80.         if(strlen($fname) > 25 || strlen($fname)<2){
  81.             array_push($error_array,"Your first name must be between 2 and 25 characters<br>");
  82.         }
  83.  
  84.         elseif(strlen($lname) > 25 || strlen($lname)<2){
  85.             array_push($error_array, "Your last name must be between 2 and 25 characters<br>");
  86.         }
  87.  
  88.         if($password!=$password2){
  89.             array_push($error_array, "Your passwords do not match<br>");
  90.         }
  91.  
  92.         else{
  93.             if(preg_match('/[^A-Za-z0-9]/', $password)){
  94.                 array_push($error_array, "You password can contain only english characters and numbers<br>");
  95.             }
  96.         }
  97.  
  98.         if(strlen($password > 30 || strlen($password) < 5)){
  99.             array_push($error_array, "Your password must be in between 5 and 30 characters<br>");
  100.         }
  101.  
  102.         if(empty($error_array)){
  103.             $password = md5($password); //Encryption of password before sending it to database
  104.  
  105.             //Generating UserName by concatenating Firstname and Lastname
  106.  
  107.             $username = strtolower($fname. "_" .$lname);
  108.             $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username = '".$username."'");  
  109.  
  110.             $i = 0;
  111.  
  112.             //if username exists add number to username
  113.             while(mysqli_num_rows($check_username_query) !=0) {
  114.                 $i++;
  115.                 $username = $username . "_" . $i;
  116.                 $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username = '".$username."'");      
  117.             }
  118.  
  119.             //Profile picture assignment
  120.  
  121.             $rand = rand(1, 2); //Random number generation between 1 and 2
  122.  
  123.             if($rand==1)
  124.                 $profile_pic = "assets/images/profile_pics/default/head_deep_blue.png";
  125.             elseif($rand==2)
  126.                 $profile_pic = "assets/images/profile_pics/default/head_emerald.png";
  127.  
  128.             $query = mysqli_query($con, "INSERT INTO users VALUES ('', '$fname', '$lname', '$username', '$em', '$password', '$date', '$profile_pic', '0', '0', 'no', ',')");
  129.         }
  130.     }
  131.  
  132.     ?>
  133.     <!DOCTYPE html>
  134.     <html>
  135.     <head>
  136.         <title>Welcome to Socialize!</title>
  137.           <link rel="icon" href="socialize_favicon.png" type="image/png" sizes="20x20">
  138.     </head>
  139.     <body>
  140.         <form action="register.php" method="post">
  141.             <input type="text" name="reg_fname" placeholder="First Name" value = "<?php
  142.             if(isset($_SESSION['reg_fname'])){
  143.                 echo ($_SESSION['reg_fname']);
  144.             }                                                                    
  145.             ?>" required><br>
  146.  
  147.             <!--Throwing error message for errors in First Name -->
  148.             <?php if(in_array("Your first name must be between 2 and 25 characters<br>",$error_array)) echo "Your first name must be between 2 and 25 characters<br>";?>
  149.  
  150.             <input type="text" name="reg_lname" placeholder="Last Name" value = "<?php
  151.             if(isset($_SESSION['reg_lname'])){
  152.                 echo ($_SESSION['reg_lname']);
  153.             }?>" required><br>
  154.  
  155.             <!--Throwing error message for errors in Last Name -->
  156.             <?php if(in_array("Your last name must be between 2 and 25 characters<br>",$error_array)) echo "Your last name must be between 2 and 25 characters<br>";?>
  157.  
  158.             <input type="email" name="reg_email" placeholder="Email" value = "<?php
  159.             if(isset($_SESSION['reg_email'])){
  160.                 echo ($_SESSION['reg_email']);
  161.             }?>" required><br>
  162.  
  163.             <input type="email" name="reg_email2" placeholder="Confirm Email"  value = "<?php
  164.             if(isset($_SESSION['reg_email2'])){
  165.                 echo ($_SESSION['reg_email2']);
  166.             }?>" required><br>
  167.  
  168.  
  169.             <!--Throwing error message for errors in Email field -->
  170.             <?php if(in_array("Email already in use<br>",$error_array)) echo "Email already in use<br>";
  171.                   elseif(in_array("Invalid email format<br>",$error_array)) echo "Invalid email format<br>";
  172.                   elseif(in_array("Emails don't match<br>",$error_array)) echo "Emails don't match<br>";
  173.             ?>
  174.  
  175.             <input type="password" name="reg_password" placeholder="Password" required><br>
  176.             <input type="password" name="reg_password2" placeholder="Confirm Password" required><br>
  177.  
  178.             <!--Throwing error message for errors in Password field -->
  179.             <?php if(in_array("Your passwords do not match<br>",$error_array)) echo "Your passwords do not match<br>";
  180.                   elseif(in_array("You password can contain only english characters and numbers<br>",$error_array)) echo "You password can contain only english characters and numbers<br>";
  181.                   elseif(in_array("Your password must be in between 5 and 30 characters<br>",$error_array)) echo "Your password must be in between 5 and 30 characters<br>";
  182.             ?>
  183.  
  184.             <input type="submit" name="register_button" value="Register"><br>
  185.         </form>
  186.  
  187.     </body>
  188.     </html>
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