Skip to content

Instantly share code, notes, and snippets.

/register.php Secret

Created August 20, 2016 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ea7340ef53523be1ade63f4a586a89ef to your computer and use it in GitHub Desktop.
Save anonymous/ea7340ef53523be1ade63f4a586a89ef to your computer and use it in GitHub Desktop.
register.php - shared from CS50 IDE
<?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