Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $con =mysqli_connect("localhost", "root","","social");
- if(mysqli_connect_errno()){
- echo "Fail to connect:" . mysqli_connect_errno();
- }
- //Declaring var to pervent errors
- $fname =""; //First name
- $lname =""; //Last name
- $em = ""; //email
- $em2 = ""; //email2
- $password = ""; //password
- $password2 = ""; //password2
- $date = ""; //Sign up date
- $error_array = array(); //Holds errors
- if(isset($_POST['register_button'])){
- // Registration form values
- //First name
- $fname = strip_tags($_POST['reg_fname']); // Remove html tags
- $fname = str_replace(' ', '', $fname); // Remove spaces
- $fname = ucfirst(strtolower($fname)); // Uppercase first letter
- $_SESSION['reg_fname'] = $fname; //Stores firstname into session variable
- //Last name
- $lname = strip_tags($_POST['reg_lname']); // Remove html tags
- $lname = str_replace(' ', '', $lname); // Remove spaces
- $lname = ucfirst(strtolower($lname)); // Uppercase first letter
- $_SESSION['reg_lname'] = $lname; //Stores lastname into session variable
- //email
- $em = strip_tags($_POST['reg_email']); // Remove html tags
- $em = str_replace(' ', '', $em); // Remove spaces
- $em = ucfirst(strtolower($em)); // Uppercase first letter
- $_SESSION['reg_email'] = $em; //Stores email into session variable
- //email 2
- $em2 = strip_tags($_POST['reg_email2']); // Remove html tags
- $em2 = str_replace(' ', '', $em2); // Remove spaces
- $em2 = ucfirst(strtolower($em2)); // Uppercase first letter
- $_SESSION['reg_email2'] = $em2; //Stores email2 into session variable
- //Password
- $password = strip_tags($_POST['reg_password']); // Remove html tags
- //Password 2
- $password2 = strip_tags($_POST['reg_password2']); // Remove html tags
- $date = date("Y-m-d"); //Current date
- if($em == $em2) {
- //Check if email invalid format
- if(filter_var($em, FILTER_VALIDATE_EMAIL)) {
- $em = filter_var($em, FILTER_VALIDATE_EMAIL);
- //Check if email already exists
- $e_check = mysqli_query($con, "SELECT email FROM users WHERE email='$em'");
- // Count the number of rows returned
- $num_rows = mysqli_num_rows($e_check);
- if($num_rows > 0) {
- array_push($error_array, "Email already in use<br>");
- }
- } else {
- array_push($error_array, "Invalid email format<br>");
- }
- } else {
- array_push($error_array, "Emails don't match<br>");
- }
- if (strlen($fname) > 25 || strlen($fname) < 2) {
- array_push($error_array, "Your frist name must be between 2 and 25 characters<br>");
- }
- if (strlen($lname) > 25 || strlen($lname) < 2) {
- array_push($error_array, "Your last name must be between 2 and 25 characters<br>");
- }
- if ($password !=$password2) {
- array_push($error_array, "Your passwords do not match<br>");
- } else {
- if(preg_match('/[^A-Za-z0-9]/', $password)) {
- array_push($error_array, "Your password can only contain english characters or numbers<br>");
- }
- }
- if(strlen($password) > 30 || strlen($password) < 6) {
- array_push($error_array, "Your password must be between 6 and 30 characters<br>");
- }
- if(empty($error_array)){
- $password = md5($password); //Encrypt password before sending to database
- // Generate username by concatenating first name and last name
- $username = strtolower($fname . "_" . $lname );
- $check_username_query = mysqli_query($con, "SELECT username FORM users WHERE username='$username'");
- $i = 0;
- //If username exists add number to username
- while(mysqli_num_rows($check_username_query)!=0){
- $i++; // Add 1 to i
- $username = $username . "_" . $i;
- $check_username_query = mysqli_query($con, "SELECT username FORM users WHERE username='$username'");
- }
- //Profile picture assignment
- $rand = rand(1,2); //Random number between 1 adn 2
- $profile_pic = "assets/images/profile_pics/head_deep_blue.png";
- if($rand == 1 )
- $profile_pic = "assets/images/profile_pics/head_deep_blue.png";
- else if($rand == 2)
- $profile_pic = "assets/images/profile_pics/head_emerald.png";
- $query = mysqli_query($con, "INSERT INTO users VALUES('', '$fname', '$lname', '$em', '$password', '$date', '$profile_pic', '0', '0', 'no', ',')");
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Welcome to Swirlfeed</title>
- </head>
- <body>
- <form action="register.php" method="POST">
- <input type="text" name="reg_fname" placeholder="First Name" value="<?php if(isset($_SESIION['reg_fname'])) {
- echo $_SESIION['reg_fname'];
- } ?>" required>
- <br>
- <?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>"?>
- <input type="text" name="reg_lname" placeholder="Last Name" value="<?php if(isset($_SESIION['reg_lname'])) {
- echo $_SESIION['reg_lname'];
- } ?>" required>
- <br>
- <?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>"?>
- <input type="email" name="reg_email" placeholder="Email" value="<?php if(isset($_SESIION['reg_email'])) {
- echo $_SESIION['reg_email'];
- } ?>" required >
- <br>
- <input type="email" name="reg_email2" placeholder="Confirm email" value="<?php if(isset($_SESIION['reg_email2'])) {
- echo $_SESIION['reg_email2'];
- } ?>" required>
- <br>
- <?php if(in_array("Email already in use<br>",$error_array)) echo "Email already in use <br>";
- else if(in_array("Invalid email format<br>",$error_array)) echo "Invalid email format<br>";
- else if(in_array("Emails don't match<br>",$error_array)) echo "Emails don't match<br>";?>
- <input type="password" name="reg_password" placeholder="password" required>
- <br>
- <input type="password" name="reg_password2" placeholder="Confirm Password" required>
- <br>
- <?php if(in_array("Your passwords do not match<br>",$error_array)) echo "Your passwords do not match<br>";
- 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>";
- 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>";?>
- <br>
- <input type="submit" name="register_button" value="register">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement