SHARE
TWEET

Untitled

a guest May 23rd, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $driver = 'mysql';
  4. $host = '127.0.0.1';
  5. $db_name = 'info';
  6. $db_user = 'root';
  7. $db_pass = '';
  8. $charset = 'utf8';
  9. $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
  10.  
  11. try {
  12.     $pdo = new PDO("$driver:host=$host;dbname=$db_name;charset=$charset",$db_user,$db_pass,$options);
  13.     session_start();
  14. } catch(PDOException $e) {
  15.     die("Something went wrong with your connection to database");
  16. }
  17.    
  18. <?php
  19. require_once 'db.php';
  20.  
  21. $login = trim(htmlentities($_POST['login']));
  22. $pwd = trim(htmlentities($_POST['password']));
  23. $email = trim(htmlentities($_POST['email']));
  24.  
  25. if( !empty($login) && !empty($pwd) && !empty($email) )
  26. {
  27.     // CHECK FOR WRONG INPUT
  28.     if( strlen($login) > 20 )
  29.     {
  30.         die("Password should be less than 20 characters.");
  31.     }
  32.     if( strlen($pwd) < 6 )
  33.     {
  34.         die("Password should be at least 6 characters long.");
  35.     }
  36.  
  37.     if( !filter_var($email, FILTER_VALIDATE_EMAIL) )
  38.     {
  39.         die("Wrong email format");
  40.     }
  41.  
  42.     $sql_check = 'SELECT EXISTS(SELECT login FROM users WHERE login = ?)';
  43.     $stmt_check = $pdo->prepare($sql_check);
  44.     $stmt_check->execute([$login]);
  45.  
  46.     if( $stmt_check->fetchColumn() )
  47.     {
  48.         die("User with this login already exists");
  49.     }
  50.  
  51.     //SIGN IN
  52.     $pwd = password_hash($pwd, PASSWORD_DEFAULT);
  53.     $sql = 'INSERT INTO users(login,password,email) VALUES(?,?,?)';
  54.     $stmt = $pdo->prepare($sql);
  55.     $params = [$login, $pwd, $email];
  56.     $stmt->execute($params);
  57.  
  58.     header("Location: ../signin.php");
  59.  
  60. } else {
  61.     echo "Wrong input";
  62. }
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