-
-
Save anonymous/ea7340ef53523be1ade63f4a586a89ef to your computer and use it in GitHub Desktop.
register.php - shared from CS50 IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// configuration | |
require("../includes/config.php"); | |
// if user reached page via GET (as by clicking a link or via redirect) | |
if ($_SERVER["REQUEST_METHOD"] == "GET") | |
{ | |
// else render form | |
render("register_form.php", ["title" => "Register"]); | |
} | |
// else if user reached page via POST (as by submitting a form via POST) | |
else if ($_SERVER["REQUEST_METHOD"] == "POST") | |
{ | |
$username = $_POST["username"]; | |
$password = $_POST["password"]; | |
$confirmation = $_POST["confirmation"]; | |
$count = query("SELECT * FROM users WHERE username = ?", $username); | |
if ($count != 0) | |
{ | |
apologize("This username already exists"); | |
exit; | |
} | |
if (empty($_POST["password"]) || empty($_POST["username"])) | |
{ | |
apologize("Username and/or password is empty"); | |
exit; | |
} | |
if ($password != $confirmation) | |
{ | |
apologize("Passwords do not match"); | |
exit; | |
} | |
$bool = CS50::query("INSERT IGNORE INTO users (username, hash, cash) VALUES(?, ?, 10000.0000)", $username, password_hash($password, PASSWORD_DEFAULT)); | |
if ($bool != 1) | |
{ | |
apologize("Could not query user information"); | |
exit; | |
} | |
$rows = CS50::query("SELECT LAST_INSERT_ID() AS id"); | |
$id = $rows[0]["id"]; | |
$_SESSION["id"] = $id; | |
redirect("index.php"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment