Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Open connection to DB by including conn.php
- include_once "conn.php";
- // Connection is On.
- /* Functions */
- function email_is_persent($con,$email)
- {
- $temp = $email;
- $conn = $con;
- $sql = "SELECT user_email FROM users WHERE user_email = ? ;";
- $stmt = mysqli_stmt_init($conn);
- mysqli_stmt_prepare($stmt,$sql);
- mysqli_stmt_bind_param($stmt,'s',$temp);
- if(!mysqli_stmt_execute($stmt)){
- echo "Error on executing stmt of email function";
- exit();
- }else{
- $result = mysqli_stmt_get_result($stmt);
- if(mysqli_num_rows($result) <= 0){
- return false;
- }else{
- return true;
- }
- }
- mysqli_stmt_close($stmt);
- }
- function username_is_persent($con,$user)
- {
- $temp = $user;
- $conn = $con;
- $sql = "SELECT user_uid FROM users WHERE user_uid = ? ;";
- $stmt = mysqli_stmt_init($conn);
- mysqli_stmt_prepare($stmt,$sql);
- mysqli_stmt_bind_param($stmt,'s',$temp);
- if(!mysqli_stmt_execute($stmt)){
- echo "Error on executing stmt of username function";
- exit();
- }else{
- $result = mysqli_stmt_get_result($stmt);
- if(mysqli_num_rows($result) <= 0){
- return false;
- }else{
- return true;
- }
- }
- mysqli_stmt_close($stmt);
- }
- function password_is_strong($password)
- {
- $temp = $password;
- $isStrong = true;
- if( strlen($temp) < 8 || !preg_match("/[A-Z]+/", $temp))
- {
- $isStrong = false;
- }
- return $isStrong;
- }
- function Signup_InsertData($con,$var_firstname,$var_lastname,$var_email,$var_phone,$var_username,$var_password)
- {
- $first = $var_firstname;
- $last =$var_lastname;
- $email = $var_email;
- $phone = $var_phone;
- $username = $var_username;
- $password = $var_password;
- $conn = $con;
- // Hashing Password For Security
- $password = password_hash($password, PASSWORD_DEFAULT);
- $sql = "INSERT INTO users (user_first,user_last,user_email,user_phone,user_uid,user_pwd) VALUES (?,?,?,?,?,?);";
- $stmt = mysqli_stmt_init($conn);
- mysqli_stmt_prepare($stmt,$sql);
- mysqli_stmt_bind_param($stmt,'ssssss',$first,$last,$email,$phone,$username,$password);
- if( !mysqli_stmt_execute($stmt) )
- {
- return false;
- }
- else
- {
- return true;
- }
- mysqli_stmt_close($stmt);
- }
- /* Functions */
- // Handles if accessing page without submit clicking
- if(!isset($_POST['SignUpSubmit'])){
- header("Location: index.php?Error=InvalidPage");
- exit();
- }
- else // If user click Submit else works
- {
- // Storing user inputs and escaping them to aviod speical characters using mysqli_real function
- $firstname = mysqli_real_escape_string($conn,$_POST['FirstName']);
- $lastname = mysqli_real_escape_string($conn,$_POST['LastName']);
- $email = mysqli_real_escape_string($conn,$_POST['Email']);
- $phone = mysqli_real_escape_string($conn,$_POST['Phone']);
- $username = mysqli_real_escape_string($conn,$_POST['UserName']);
- $password = mysqli_real_escape_string($conn,$_POST['Password']);
- // Checking if all fields are filled
- if( empty($firstname) or empty($lastname) or empty($email) or empty($phone) or empty($username) or empty($password) )
- {
- // Including all inputs (except for password) , back on the URL to retrieve them using $_GET[] at index.php
- header("Location: index.php?Error=EmptyField&firstname=$firstname&lastname=$lastname&email=$email&phone=$phone&username=$username");
- exit();
- }
- else // if all fields arn't empty else works
- {
- if( !preg_match("/^[A-Za-z]+$/", $firstname) or !preg_match("/^[A-Za-z]+$/", $lastname) ) // Validating FirstName and LastName Formats
- {
- header("Location: index.php?Error=InvalidName&firstname=$firstname&lastname=$lastname&email=$email&phone=$phone&username=$username");
- exit();
- }
- else // if FirstName and LastName are valid else works
- {
- if( !filter_var($email,FILTER_VALIDATE_EMAIL) ) // Validate Email Format
- {
- header("Location: index.php?Error=InvalidEmail&firstname=$firstname&lastname=$lastname&email=&phone=$phone&username=$username");
- exit();
- }
- else // if Email is valid format else works
- {
- if( email_is_persent($conn,$email)) // Check if email is already persent on DB
- {
- header("Location: index.php?Error=PersentEmail&firstname=$firstname&lastname=$lastname&email=&phone=$phone&username=$username");
- exit();
- }
- else // if Email is not persent at DB else works
- {
- if( !preg_match("/^01[0-9]{8}$/", $phone) ) // Checks if phone format is valid
- {
- header("Location: index.php?Error=InvalidPhone&firstname=$firstname&lastname=$lastname&email=$email&phone=&username=$username");
- exit();
- }
- else // if phone format is valid else works
- {
- if( username_is_persent($conn,$username) ) // Checks if username is already persent on DB
- {
- header("Location: index.php?Error=PersentUsername&firstname=$firstname&lastname=$lastname&email=$email&phone=$phone&username=");
- exit();
- }
- else // if username is not persent at DB else works
- {
- if( !password_is_strong($password) ) // Check password Strength
- {
- header("Location: index.php?Error=WeakPassword&firstname=$firstname&lastname=$lastname&email=$email&phone=$phone&username=$username");
- exit();
- }
- else // if password is strong else works
- {
- if( !Signup_InsertData($conn,$firstname,$lastname,$email,$phone,$username,$password) ) // Function to Complete Signup
- {
- header("Location: index.php?Error=Signup&firstname=$firstname&lastname=$lastname&email=$email&phone=$phone&username=$username");
- exit();
- }
- else // if Signup is successfull else works
- {
- echo "Your Signed Up Successfully!";
- mysql_close($conn);
- }
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement