Skip to content

Commit 660e4f1

Browse files
authored
Add files via upload
1 parent 387f677 commit 660e4f1

15 files changed

+612
-0
lines changed

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"phpmailer/phpmailer": "^6.8"
4+
}
5+
}

composer.lock

+99
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$host = "localhost";
4+
$dbname = "login_db";
5+
$username = "root";
6+
$password = "";
7+
8+
$mysqli = new mysqli(hostname: $host,
9+
username: $username,
10+
password: $password,
11+
database: $dbname);
12+
13+
if ($mysqli->connect_errno) {
14+
die("Connection error: " . $mysqli->connect_error);
15+
}
16+
17+
return $mysqli;

forgot-password.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Forgot Password</title>
5+
<meta charset="UTF-8">
6+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
7+
</head>
8+
<body>
9+
10+
<h1>Forgot Password</h1>
11+
12+
<form method="post" action="send-password-reset.php">
13+
14+
<label for="email">email</label>
15+
<input type="email" name="email" id="email">
16+
17+
<button>Send</button>
18+
19+
</form>
20+
21+
</body>
22+
</html>

index.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
session_start();
4+
5+
if (isset($_SESSION["user_id"])) {
6+
7+
$mysqli = require __DIR__ . "/database.php";
8+
9+
$sql = "SELECT * FROM user
10+
WHERE id = {$_SESSION["user_id"]}";
11+
12+
$result = $mysqli->query($sql);
13+
14+
$user = $result->fetch_assoc();
15+
}
16+
17+
?>
18+
<!DOCTYPE html>
19+
<html>
20+
<head>
21+
<title>Home</title>
22+
<meta charset="UTF-8">
23+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
24+
</head>
25+
<body>
26+
27+
<h1>Home</h1>
28+
29+
<?php if (isset($user)): ?>
30+
31+
<p>Hello <?= htmlspecialchars($user["name"]) ?></p>
32+
33+
<p><a href="logout.php">Log out</a></p>
34+
35+
<?php else: ?>
36+
37+
<p><a href="login.php">Log in</a> or <a href="signup.html">sign up</a></p>
38+
39+
<?php endif; ?>
40+
41+
</body>
42+
</html>
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+

login.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
$is_invalid = false;
4+
5+
if ($_SERVER["REQUEST_METHOD"] === "POST") {
6+
7+
$mysqli = require __DIR__ . "/database.php";
8+
9+
$sql = sprintf("SELECT * FROM user
10+
WHERE email = '%s'",
11+
$mysqli->real_escape_string($_POST["email"]));
12+
13+
$result = $mysqli->query($sql);
14+
15+
$user = $result->fetch_assoc();
16+
17+
if ($user) {
18+
19+
if (password_verify($_POST["password"], $user["password_hash"])) {
20+
21+
session_start();
22+
23+
session_regenerate_id();
24+
25+
$_SESSION["user_id"] = $user["id"];
26+
27+
header("Location: index.php");
28+
exit;
29+
}
30+
}
31+
32+
$is_invalid = true;
33+
}
34+
35+
?>
36+
<!DOCTYPE html>
37+
<html>
38+
<head>
39+
<title>Login</title>
40+
<meta charset="UTF-8">
41+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
42+
</head>
43+
<body>
44+
45+
<h1>Login</h1>
46+
47+
<?php if ($is_invalid): ?>
48+
<em>Invalid login</em>
49+
<?php endif; ?>
50+
51+
<form method="post">
52+
<label for="email">email</label>
53+
<input type="email" name="email" id="email"
54+
value="<?= htmlspecialchars($_POST["email"] ?? "") ?>">
55+
56+
<label for="password">Password</label>
57+
<input type="password" name="password" id="password">
58+
59+
<button>Log in</button>
60+
</form>
61+
62+
<a href="forgot-password.php">Forgot password?</a>
63+
64+
</body>
65+
</html>
66+
67+
68+
69+
70+
71+
72+
73+

logout.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
session_start();
4+
5+
session_destroy();
6+
7+
header("Location: index.php");
8+
exit;

mailer.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use PHPMailer\PHPMailer\PHPMailer;
4+
use PHPMailer\PHPMailer\SMTP;
5+
use PHPMailer\PHPMailer\Exception;
6+
7+
require __DIR__ . "/vendor/autoload.php";
8+
9+
$mail = new PHPMailer(true);
10+
11+
// $mail->SMTPDebug = SMTP::DEBUG_SERVER;
12+
13+
$mail->isSMTP();
14+
$mail->SMTPAuth = true;
15+
16+
$mail->Host = "smtp.example.com";
17+
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
18+
$mail->Port = 587;
19+
$mail->Username = "your-user@example.com";
20+
$mail->Password = "your-password";
21+
22+
$mail->isHtml(true);
23+
24+
return $mail;

process-reset-password.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
$token = $_POST["token"];
4+
5+
$token_hash = hash("sha256", $token);
6+
7+
$mysqli = require __DIR__ . "/database.php";
8+
9+
$sql = "SELECT * FROM user
10+
WHERE reset_token_hash = ?";
11+
12+
$stmt = $mysqli->prepare($sql);
13+
14+
$stmt->bind_param("s", $token_hash);
15+
16+
$stmt->execute();
17+
18+
$result = $stmt->get_result();
19+
20+
$user = $result->fetch_assoc();
21+
22+
if ($user === null) {
23+
die("token not found");
24+
}
25+
26+
if (strtotime($user["reset_token_expires_at"]) <= time()) {
27+
die("token has expired");
28+
}
29+
30+
if (strlen($_POST["password"]) < 8) {
31+
die("Password must be at least 8 characters");
32+
}
33+
34+
if ( ! preg_match("/[a-z]/i", $_POST["password"])) {
35+
die("Password must contain at least one letter");
36+
}
37+
38+
if ( ! preg_match("/[0-9]/", $_POST["password"])) {
39+
die("Password must contain at least one number");
40+
}
41+
42+
if ($_POST["password"] !== $_POST["password_confirmation"]) {
43+
die("Passwords must match");
44+
}
45+
46+
$password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT);
47+
48+
$sql = "UPDATE user
49+
SET password_hash = ?,
50+
reset_token_hash = NULL,
51+
reset_token_expires_at = NULL
52+
WHERE id = ?";
53+
54+
$stmt = $mysqli->prepare($sql);
55+
56+
$stmt->bind_param("ss", $password_hash, $user["id"]);
57+
58+
$stmt->execute();
59+
60+
echo "Password updated. You can now login.";

0 commit comments

Comments
 (0)