SHARE
TWEET

Untitled

a guest Jan 26th, 2019 163 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. session_start();
  3. $con =mysqli_connect("localhost", "root","","social");
  4.  
  5. if(mysqli_connect_errno()){
  6.     echo "Fail to connect:" . mysqli_connect_errno();
  7. }
  8.  
  9. //Declaring var to pervent errors
  10.  
  11. $fname =""; //First name
  12. $lname =""; //Last name
  13. $em = ""; //email
  14. $em2 = ""; //email2
  15. $password = ""; //password
  16. $password2 = ""; //password2
  17. $date = ""; //Sign up date
  18. $error_array = array(); //Holds errors
  19.  
  20. if(isset($_POST['register_button'])){
  21.    
  22.     // Registration form values
  23.  
  24.     //First name
  25.     $fname = strip_tags($_POST['reg_fname']); // Remove html tags
  26.     $fname = str_replace(' ', '', $fname); // Remove spaces
  27.     $fname = ucfirst(strtolower($fname)); // Uppercase first letter
  28.     $_SESSION['reg_fname'] = $fname; //Stores firstname into session variable
  29.  
  30.     //Last name
  31.     $lname = strip_tags($_POST['reg_lname']); // Remove html tags
  32.     $lname = str_replace(' ', '', $lname); // Remove spaces
  33.     $lname = ucfirst(strtolower($lname)); // Uppercase first letter
  34.     $_SESSION['reg_lname'] = $lname; //Stores lastname into session variable
  35.  
  36.      //email
  37.      $em = strip_tags($_POST['reg_email']); // Remove html tags
  38.      $em = str_replace(' ', '', $em); // Remove spaces
  39.      $em = ucfirst(strtolower($em)); // Uppercase first letter
  40.      $_SESSION['reg_email'] = $em; //Stores email into session variable
  41.  
  42.      //email 2
  43.      $em2 = strip_tags($_POST['reg_email2']); // Remove html tags
  44.      $em2 = str_replace(' ', '', $em2); // Remove spaces
  45.      $em2 = ucfirst(strtolower($em2)); // Uppercase first letter
  46.      $_SESSION['reg_email2'] = $em2; //Stores email2 into session variable
  47.  
  48.      //Password
  49.      $password = strip_tags($_POST['reg_password']); // Remove html tags
  50.      //Password 2
  51.      $password2 = strip_tags($_POST['reg_password2']); // Remove html tags
  52.  
  53.      $date = date("Y-m-d");  //Current date
  54.  
  55.     if($em == $em2) {
  56.         //Check if email invalid 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 the 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.     if (strlen($fname) > 25 || strlen($fname) < 2) {
  79.         array_push($error_array,  "Your frist name must be between 2 and 25 characters<br>");
  80.     }
  81.    
  82.     if (strlen($lname) > 25 || strlen($lname) < 2) {
  83.         array_push($error_array, "Your last name must be between 2 and 25 characters<br>");
  84.     }
  85.  
  86.     if ($password !=$password2) {
  87.         array_push($error_array, "Your passwords do not match<br>");
  88.     } else {
  89.        if(preg_match('/[^A-Za-z0-9]/', $password)) {
  90.         array_push($error_array, "Your password can only contain english characters or numbers<br>");
  91.        }
  92.     }
  93.  
  94.     if(strlen($password) > 30 || strlen($password) < 6) {
  95.         array_push($error_array,  "Your password must be between 6 and 30 characters<br>");
  96.     }
  97.  
  98.     if(empty($error_array)){
  99.         $password = md5($password); //Encrypt password before sending to database
  100.         // Generate username by concatenating first name and last name
  101.         $username = strtolower($fname . "_" . $lname );
  102.         $check_username_query = mysqli_query($con, "SELECT username FORM users WHERE username='$username'");
  103.  
  104.         $i = 0;
  105.         //If username exists add number to username
  106.         while(mysqli_num_rows($check_username_query)!=0){
  107.             $i++; // Add 1 to i
  108.             $username = $username . "_" . $i;
  109.             $check_username_query = mysqli_query($con, "SELECT username FORM users WHERE username='$username'");
  110.         }
  111.         //Profile picture assignment
  112.         $rand = rand(1,2); //Random number between 1 adn 2
  113.         $profile_pic = "assets/images/profile_pics/head_deep_blue.png";
  114.         if($rand == 1 )
  115.             $profile_pic = "assets/images/profile_pics/head_deep_blue.png";
  116.         else if($rand == 2)
  117.         $profile_pic = "assets/images/profile_pics/head_emerald.png";
  118.        
  119.         $query = mysqli_query($con, "INSERT INTO users VALUES('', '$fname', '$lname', '$em', '$password', '$date', '$profile_pic', '0', '0', 'no', ',')");
  120.     }
  121.  
  122. }
  123.  
  124. ?>
  125.  
  126.  
  127. <!DOCTYPE html>
  128. <html lang="en">
  129. <head>
  130.     <meta charset="UTF-8">
  131.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  132.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  133.     <title>Welcome to Swirlfeed</title>
  134. </head>
  135. <body>
  136.    
  137.     <form action="register.php" method="POST">
  138.         <input type="text" name="reg_fname" placeholder="First Name" value="<?php if(isset($_SESIION['reg_fname'])) {
  139.            echo  $_SESIION['reg_fname'];
  140.         } ?>" required>
  141.         <br>
  142.         <?php if(in_array("Your frist name must be between 2 and 25 characters<br>", $error_array)) echo "Your frist name must be betweeb 2 and 25 characters<br>"?>
  143.         <input type="text" name="reg_lname" placeholder="Last Name" value="<?php if(isset($_SESIION['reg_lname'])) {
  144.            echo  $_SESIION['reg_lname'];
  145.         } ?>" required>
  146.         <br>
  147.         <?php if(in_array("Your last name must be between 2 and 25 characters<br>", $error_array)) echo "Your last name must be betweeb 2 and 25 characters<br>"?>
  148.         <input type="email" name="reg_email" placeholder="Email" value="<?php if(isset($_SESIION['reg_email'])) {
  149.            echo  $_SESIION['reg_email'];
  150.         } ?>" required >
  151.         <br>
  152.        
  153.         <input type="email" name="reg_email2" placeholder="Confirm email" value="<?php if(isset($_SESIION['reg_email2'])) {
  154.            echo  $_SESIION['reg_email2'];
  155.         } ?>" required>
  156.         <br>
  157.         <?php if(in_array("Email already in use<br>",$error_array))  echo "Email already in use <br>";
  158.         else if(in_array("Invalid email format<br>",$error_array))  echo "Invalid email format<br>";
  159.         else if(in_array("Emails don't match<br>",$error_array))  echo "Emails don't match<br>";?>
  160.         <input type="password" name="reg_password" placeholder="password" required>
  161.         <br>
  162.         <input type="password" name="reg_password2" placeholder="Confirm Password" required>
  163.         <br>
  164.         <?php if(in_array("Your passwords do not match<br>",$error_array))  echo "Your passwords do not match<br>";
  165.         else if(in_array("Your password can only contain english characters or numbers<br>",$error_array))  echo "Your password can only contain english characters or numbers<br>";
  166.         else if(in_array("Your password must be between 6 and 30 characters<br>",$error_array))  echo "Your password must be between 6 and 30 characters<br>";?>
  167.         <br>
  168.         <input type="submit" name="register_button" value="register">
  169.     </form>
  170.  
  171. </body>
  172. </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