Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2. session_start();
  3. class register
  4. {
  5.     private static function db()
  6.     {
  7.         $password = '';
  8.         $mysqli = new mysqli('localhost', 'root', $password, 'test');
  9.         if ($mysqli->connect_error)
  10.         {
  11.             die("MySQL error1: ". htmlspecialchars($mysqli->connect_error , ENT_QUOTES, 'big5'));
  12.         }
  13.         return $mysqli;
  14.     }
  15.    
  16.     public static function registerUser(): bool
  17.     {
  18.         $success = true;
  19.         if (!self::verifyPassword($_POST['register_pass'], $_POST['register_pass_check']))
  20.         {
  21.             echo "Password does not match";
  22.             $success = false;
  23.         }
  24.        
  25.         $username = self::db()->escape_string($_POST['register_id']);
  26.         $password_hash = password_hash($_POST['register_pass'], PASSWORD_DEFAULT);
  27.  
  28.         if (!self::writeUserToDatabase($username, $password_hash)) {
  29.             echo "Account creation failed";
  30.             $success = false;
  31.         }else{
  32.             echo "Account creation success";
  33.         }
  34.         return $success;
  35.     }
  36.    
  37.     public static function verifyPassword(string $pass, string $pass_verify): bool
  38.     {
  39.         if ($pass !== $pass_verify)
  40.         {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.    
  46.     public static function writeUserToDatabase(string $username, string $password_hash): bool
  47.     {
  48.         $mysqli = new mysqli('localhost', 'root', '', 'test');
  49.         $sql = "INSERT INTO user (username, password) VALUES(?,?)";
  50.         //$stmt = self::db()->prepare($sql);
  51.         $stmt = $mysqli->prepare($sql);
  52.         $stmt->bind_param("ss", $username, $password_hash);
  53.         $stmt->execute();
  54.         //mysqli_stmt_execute($stmt);
  55.         if (!$stmt->execute())
  56.         {
  57.             die("MySQL error2: ". htmlspecialchars($stmt->error , ENT_QUOTES, 'big5'));
  58.         }
  59.         $stmt->close();
  60.         return true;
  61.     }
  62. }
  63. ?>
  64.  
  65. <html>
  66.     <head>
  67.         <meta http-equiv="Content-Type" content="text/html; charset=big5">
  68.     </head>
  69.     <body align="center" valign="center">
  70.         <form action="register.php" method="post">
  71.             <label>Username:</label>
  72.             <input name="register_id" type="text" required/><br/>
  73.             <label>Password:</label>
  74.             <input name="register_pass" type="text" required/><br/>
  75.             <label>Password2:</label>
  76.             <input name="register_pass_check" type="text" required/><br/>
  77.             <button type="submit">Register</button>
  78.         </form>
  79.         <?php
  80.             header('Content-type: text/html; charset=big5');
  81.             if (isset($_POST['register_id']))
  82.             {
  83.                 Register::registerUser();
  84.             }
  85.         ?>
  86.     </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement