Skip to content

Commit 748aaa4

Browse files
User login (#1)
* Bare login form * Empty homepage with login link * Simple user table schema * Added registration page * Registration with salted+hashed passwords * Added non-null requirements to user schema * Require unique usernames * Register and login prototype working
1 parent 8ffa55e commit 748aaa4

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web/db-login.php

schema/user.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE `user` (
2+
`user_id` int NOT NULL AUTO_INCREMENT,
3+
`username` varchar(64) NOT NULL,
4+
`password_hash` binary(32) NOT NULL, -- SHA-256 produces a 32-byte digest
5+
`salt` binary(16) NOT NULL,
6+
PRIMARY KEY (`user_id`),
7+
UNIQUE KEY `username` (`username`)
8+
) ENGINE=InnoDB DEFAULT CHARSET=ascii COLLATE=ascii_bin;

web/index.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Document</title>
7+
</head>
8+
9+
<body>
10+
<h1>Home</h1>
11+
<p>
12+
<a href="login.php">Login</a>
13+
</p>
14+
<p>
15+
<a href="register.php">Register</a>
16+
</p>
17+
</body>
18+
19+
</html>

web/login.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
require_once "db-login.php";
3+
4+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
5+
$username = $_POST["username"];
6+
$password = $_POST["password"];
7+
8+
$query = $mysql->prepare(
9+
"SELECT
10+
user_id,
11+
username,
12+
password_hash,
13+
salt
14+
FROM
15+
user
16+
WHERE
17+
username=?
18+
"
19+
);
20+
$query->bind_param("s", $username);
21+
$query->execute();
22+
$result = $query->get_result();
23+
24+
if (
25+
$result
26+
&& ($user = $result->fetch_object())
27+
&& hash("sha256", $user->salt . $password, true) === $user->password_hash
28+
) {
29+
echo "Welcome {$user->username}!";
30+
} else {
31+
echo "Incorrect username or password";
32+
}
33+
}
34+
?>
35+
<!DOCTYPE html>
36+
<html lang="en">
37+
38+
<head>
39+
<meta charset="UTF-8">
40+
<title>Login</title>
41+
</head>
42+
43+
<body>
44+
<h1>Login</h1>
45+
<form action="login.php" method="post">
46+
<label for="input-username">
47+
Username
48+
<input type="text" name="username" id="input-username">
49+
</label>
50+
<label for="input-password">
51+
Password
52+
<input type="password" name="password" id="input-password">
53+
</label>
54+
<input type="submit" value="Login">
55+
</form>
56+
</body>
57+
58+
</html>

web/register.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
require_once "db-login.php";
3+
4+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
5+
$username = $_POST["username"];
6+
$password = $_POST["password"];
7+
8+
$salt = random_bytes(16);
9+
$password_hash = hash("sha256", $salt . $password, true);
10+
11+
$query = $mysql->prepare(
12+
"INSERT INTO user(
13+
username,
14+
password_hash,
15+
salt
16+
) VALUES (
17+
?, ?, ?
18+
)"
19+
);
20+
$query->bind_param("sss", $username, $password_hash, $salt);
21+
if ($query->execute())
22+
echo "Success";
23+
else
24+
echo "Failure";
25+
}
26+
?>
27+
<!DOCTYPE html>
28+
<html lang="en">
29+
30+
<head>
31+
<meta charset="UTF-8">
32+
<title>Register</title>
33+
</head>
34+
35+
<body>
36+
<h1>Register</h1>
37+
<form action="register.php" method="post">
38+
<label for="input-username">
39+
Username
40+
<input type="text" name="username" id="input-username">
41+
</label>
42+
<label for="input-password">
43+
Password
44+
<input type="password" name="password" id="input-password">
45+
</label>
46+
<input type="submit" value="Login">
47+
</form>
48+
</body>
49+
50+
</html>

0 commit comments

Comments
 (0)