Advertisement
Guest User

Untitled

a guest
Sep 30th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.02 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once("config.php");
  4.  
  5. $username = $password = "";
  6. $username_err = $password_err = "";
  7.  
  8. // Processing form data when form is submitted
  9. if($_SERVER["REQUEST_METHOD"] == "POST"){
  10.  
  11.     // Validate username
  12.     if(empty(trim($_POST["username"]))){
  13.         $username_err = "Nera vardo";
  14.     } else{
  15.         // Prepare a select statement
  16.         $sql = "SELECT id FROM users WHERE username = :username";
  17.  
  18.         if($stmt = $pdo->prepare($sql)){
  19.             // Bind variables to the prepared statement as parameters
  20.             $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
  21.  
  22.             // Set parameters
  23.             $param_username = trim($_POST["username"]);
  24.  
  25.             // Attempt to execute the prepared statement
  26.             if($stmt->execute()){
  27.                 if($stmt->rowCount() == 1){
  28.                     $username_err = "Vardas naudojamas.";
  29.                 } else{
  30.                     $username = trim($_POST["username"]);
  31.                 }
  32.             } else{
  33.                 echo "Oops! Something went wrong. Please try again later.";
  34.             }
  35.         }
  36.  
  37.         // Close statement
  38.         unset($stmt);
  39.     }
  40.  
  41.     // Validate password
  42.     if(empty(trim($_POST["password"]))){
  43.         $password_err = "No pass";
  44.     } elseif(strlen(trim($_POST["password"])) < 6){
  45.         $password_err = "Pass min 6 symbols";
  46.     } else{
  47.         $password = trim($_POST["password"]);
  48.     }
  49.  
  50.     // Validate confirm password
  51.     if(empty(trim($_POST["confirm_password"]))){
  52.         $confirm_password_err = "Confirm pass";
  53.     } else{
  54.         $confirm_password = trim($_POST["confirm_password"]);
  55.         if(empty($password_err) && ($password != $confirm_password)){
  56.             $confirm_password_err = "Password did not match.";
  57.         }
  58.   }
  59.  
  60.     // Check input errors before inserting in database
  61.     if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  62.  
  63.         // Prepare an insert statement
  64.         $sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
  65.  
  66.         if($stmt = $pdo->prepare($sql)){
  67.             // Bind variables to the prepared statement as parameters
  68.             $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
  69.             $stmt->bindParam(":password", $param_password, PDO::PARAM_STR);
  70.  
  71.             // Set parameters
  72.             $param_username = $username;
  73.             $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  74.  
  75.             // Attempt to execute the prepared statement
  76.             if($stmt->execute()){
  77.                 // Redirect to login page
  78.                // header("location: login.php");
  79.                 echo "1";
  80.             } else{
  81.                 echo "Something went wrong. Please try again later.";
  82.             }
  83.         }
  84.  
  85.         // Close statement
  86.         unset($stmt);
  87.     }
  88.    echo $username_err;
  89.    echo $password_err;
  90.    echo $confirm_password_err;
  91.     // Close connection
  92.     unset($pdo);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement