Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once("config.php");
- $username = $password = "";
- $username_err = $password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- // Validate username
- if(empty(trim($_POST["username"]))){
- $username_err = "Nera vardo";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE username = :username";
- if($stmt = $pdo->prepare($sql)){
- // Bind variables to the prepared statement as parameters
- $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
- // Set parameters
- $param_username = trim($_POST["username"]);
- // Attempt to execute the prepared statement
- if($stmt->execute()){
- if($stmt->rowCount() == 1){
- $username_err = "Vardas naudojamas.";
- } else{
- $username = trim($_POST["username"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // Close statement
- unset($stmt);
- }
- // Validate password
- if(empty(trim($_POST["password"]))){
- $password_err = "No pass";
- } elseif(strlen(trim($_POST["password"])) < 6){
- $password_err = "Pass min 6 symbols";
- } else{
- $password = trim($_POST["password"]);
- }
- // Validate confirm password
- if(empty(trim($_POST["confirm_password"]))){
- $confirm_password_err = "Confirm pass";
- } else{
- $confirm_password = trim($_POST["confirm_password"]);
- if(empty($password_err) && ($password != $confirm_password)){
- $confirm_password_err = "Password did not match.";
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
- // Prepare an insert statement
- $sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
- if($stmt = $pdo->prepare($sql)){
- // Bind variables to the prepared statement as parameters
- $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
- $stmt->bindParam(":password", $param_password, PDO::PARAM_STR);
- // Set parameters
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- // Attempt to execute the prepared statement
- if($stmt->execute()){
- // Redirect to login page
- // header("location: login.php");
- echo "1";
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- unset($stmt);
- }
- echo $username_err;
- echo $password_err;
- echo $confirm_password_err;
- // Close connection
- unset($pdo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement