Skip to content

Commit 626c541

Browse files
committed
Finish log-in system
1 parent 347c669 commit 626c541

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

data/data.sqlite

4 KB
Binary file not shown.

index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
require_once 'lib/common.php';
33

4+
session_start();
5+
46
// Connect to the database, run a query, handle errors
57
$pdo = getPDO();
68
$stmt = $pdo->query(

lib/common.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,55 @@ function getCommentsForPost($postId)
131131
array('post_id' => $postId, )
132132
);
133133
return $stmt->fetchAll(PDO::FETCH_ASSOC);
134+
}
135+
136+
function tryLogin(PDO $pdo, $username, $password)
137+
{
138+
$sql = "
139+
SELECT
140+
password
141+
FROM
142+
user
143+
WHERE
144+
username = :username
145+
";
146+
$stmt = $pdo->prepare($sql);
147+
$stmt->execute(
148+
array('username' => $username, )
149+
);
150+
// Get the hash from this row, and use the third-party hashing library to check it
151+
$hash = $stmt->fetchColumn();
152+
$success = password_verify($password, $hash);
153+
return $success;
154+
}
155+
/**
156+
* Logs the user in
157+
*
158+
* For safety, we ask PHP to regenerate the cookie, so if a user logs onto a site that a cracker
159+
* has prepared for him/her (e.g. on a public computer) the cracker's copy of the cookie ID will be
160+
* useless.
161+
*
162+
* @param string $username
163+
*/
164+
function login($username)
165+
{
166+
session_regenerate_id();
167+
$_SESSION['logged_in_username'] = $username;
168+
}
169+
170+
/**
171+
* Logs the user out
172+
*/
173+
function logout()
174+
{
175+
unset($_SESSION['logged_in_username']);
176+
}
177+
function getAuthUser()
178+
{
179+
return isLoggedIn() ? $_SESSION['logged_in_username'] : null;
180+
}
181+
182+
function isLoggedIn()
183+
{
184+
return isset($_SESSION['logged_in_username']);
134185
}

login.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
<?php
2+
require_once 'lib/common.php';
3+
// We need to test for a minimum version of PHP, because earlier versions have bugs that affect security
4+
if (version_compare(PHP_VERSION, '5.3.7') < 0)
5+
{
6+
throw new Exception(
7+
'This system needs PHP 5.3.7 or later'
8+
);
9+
}
10+
11+
session_start();
12+
13+
// Handle the form posting
14+
$username = '';
15+
if ($_POST)
16+
{
17+
// Init the database
18+
$pdo = getPDO();
19+
// We redirect only if the password is correct
20+
$username = $_POST['username'];
21+
$ok = tryLogin($pdo, $username, $_POST['password']);
22+
if ($ok)
23+
{
24+
login($username);
25+
redirectAndExit('index.php');
26+
}
27+
}
28+
?>
129
<!DOCTYPE html>
230
<html>
331
<head>
@@ -8,13 +36,25 @@
836
</head>
937
<body>
1038
<?php require 'templates/title.php' ?>
39+
40+
<?php // If we have a username, then the user got something wrong, so let's have an error ?>
41+
<?php if ($username): ?>
42+
<div style="border: 1px solid #ff6666; padding: 6px;">
43+
The username or password is incorrect, try again
44+
</div>
45+
<?php endif ?>
46+
1147
<p>Login here:</p>
1248
<form
1349
method="post"
1450
>
1551
<p>
1652
Username:
17-
<input type="text" name="username" />
53+
<input
54+
type="text"
55+
name="username"
56+
value="<?php echo htmlEscape($username) ?>"
57+
/>
1858
</p>
1959
<p>
2060
Password:

logout.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once 'lib/common.php';
3+
session_start();
4+
logout();
5+
redirectAndExit('index.php');

templates/title.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<div style="float: right;">
2-
<a href="login.php">Log in</a>
2+
<?php if (isLoggedIn()): ?>
3+
Hello <?php echo htmlEscape(getAuthUser()) ?>.
4+
<a href="logout.php">Log out</a>
5+
<?php else: ?>
6+
<a href="login.php">Log in</a>
7+
<?php endif ?>
38
</div>
49

510
<a href="index.php">

view-post.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require_once 'lib/common.php';
33
require_once 'lib/view-post.php';
44

5+
session_start();
6+
57
// Get the post ID
68
if (isset($_GET['post_id']))
79
{
@@ -91,5 +93,7 @@
9193
</div>
9294
</div>
9395
<?php endforeach ?>
96+
97+
<?php require 'templates/comment-form.php' ?>
9498
</body>
9599
</html>

0 commit comments

Comments
 (0)