Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- class register
- {
- private static function db()
- {
- $password = '';
- $mysqli = new mysqli('localhost', 'root', $password, 'test');
- if ($mysqli->connect_error)
- {
- die("MySQL error1: ". htmlspecialchars($mysqli->connect_error , ENT_QUOTES, 'big5'));
- }
- return $mysqli;
- }
- public static function registerUser(): bool
- {
- $success = true;
- if (!self::verifyPassword($_POST['register_pass'], $_POST['register_pass_check']))
- {
- echo "Password does not match";
- $success = false;
- }
- $username = self::db()->escape_string($_POST['register_id']);
- $password_hash = password_hash($_POST['register_pass'], PASSWORD_DEFAULT);
- if (!self::writeUserToDatabase($username, $password_hash)) {
- echo "Account creation failed";
- $success = false;
- }else{
- echo "Account creation success";
- }
- return $success;
- }
- public static function verifyPassword(string $pass, string $pass_verify): bool
- {
- if ($pass !== $pass_verify)
- {
- return false;
- }
- return true;
- }
- public static function writeUserToDatabase(string $username, string $password_hash): bool
- {
- $mysqli = new mysqli('localhost', 'root', '', 'test');
- $sql = "INSERT INTO user (username, password) VALUES(?,?)";
- //$stmt = self::db()->prepare($sql);
- $stmt = $mysqli->prepare($sql);
- $stmt->bind_param("ss", $username, $password_hash);
- $stmt->execute();
- //mysqli_stmt_execute($stmt);
- if (!$stmt->execute())
- {
- die("MySQL error2: ". htmlspecialchars($stmt->error , ENT_QUOTES, 'big5'));
- }
- $stmt->close();
- return true;
- }
- }
- ?>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=big5">
- </head>
- <body align="center" valign="center">
- <form action="register.php" method="post">
- <label>Username:</label>
- <input name="register_id" type="text" required/><br/>
- <label>Password:</label>
- <input name="register_pass" type="text" required/><br/>
- <label>Password2:</label>
- <input name="register_pass_check" type="text" required/><br/>
- <button type="submit">Register</button>
- </form>
- <?php
- header('Content-type: text/html; charset=big5');
- if (isset($_POST['register_id']))
- {
- Register::registerUser();
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement