Skip to content

Commit 83b8bc3

Browse files
committed
Only set session cookies on login
Be stingy with our session cookies and only set them on successful login rather than just with a page request. This improves our ability to shortcut code for crawlers that retain cookies.
1 parent 2bae0b6 commit 83b8bc3

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

‎pinc/dpsession.inc‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,42 @@ function dpsession_begin(string $userID): void
3636
* Resume a session, if one exists.
3737
*
3838
* If the request claimed to belong to a session, and that session is
39-
* valid/current/non-expired, then refresh the session, reinstate the
40-
* global variable ($pguser) of that session, and return TRUE.
41-
* Otherwise, return FALSE.
39+
* valid/current/non-expired, then refresh the session, and reinstate the
40+
* global variable ($pguser) of that session.
4241
*/
4342
function dpsession_resume(): void
4443
{
4544
global $cookie_lifetime, $pguser, $session_name;
4645

46+
// If there's no cookie they can't be logged in and there's nothing
47+
// to resume and don't bother sending them a session cookie.
48+
if (!dpsession_is_cookie_set()) {
49+
// If this is the first time in this browsing session that they're
50+
// hitting the site, we grab the request's http_referer (if any),
51+
// and stash that in a cookie for later use in case they register
52+
// in this browsing session.
53+
if (!isset($_COOKIE["http_referer"]) && $referer = $_SERVER["HTTP_REFERER"] ?? '') {
54+
dp_setcookie('http_referer', $referer);
55+
}
56+
57+
return;
58+
}
59+
4760
_session_set_handlers_and_start();
4861

49-
if (isset($_SESSION['pguser']) && !empty($_SESSION['pguser'])) {
62+
// If it's a valid session, refresh the cookie and update user activity
63+
if (!empty($_SESSION['pguser'] ?? '')) {
5064
// Refresh the cookie
5165
if (($id = session_id()) !== false) {
5266
dp_setcookie($session_name, $id, time() + $cookie_lifetime);
5367
}
5468

5569
// set global variable $pguser
5670
$pguser = $_SESSION['pguser'];
57-
$user_is_logged_in = true;
58-
} else {
59-
session_unset();
60-
session_destroy();
61-
$user_is_logged_in = false;
62-
}
6371

64-
if ($user_is_logged_in) {
6572
_update_user_activity_time(false);
66-
}
67-
68-
// If the sender isn't logged in, and this is the first time in this
69-
// browsing session that they're hitting the site, we grab the request's
70-
// http_referer (if any), and stash that in a cookie for later use, in
71-
// case they register in this browsing session.
72-
if (!$user_is_logged_in && !isset($_COOKIE["http_referer"])) {
73-
if (isset($_SERVER["HTTP_REFERER"])) {
74-
dp_setcookie('http_referer', $_SERVER["HTTP_REFERER"]);
75-
} else {
76-
dp_setcookie('http_referer', '');
77-
}
73+
} else {
74+
dpsession_end();
7875
}
7976
}
8077

@@ -255,6 +252,12 @@ class DPSessionHandler implements SessionHandlerInterface
255252
}
256253
}
257254

255+
/*
256+
* Start PHP session management.
257+
*
258+
* This will create the session handler and start the session which will
259+
* create a session in the DB and send the user a session cookie.
260+
*/
258261
function _session_set_handlers_and_start(): void
259262
{
260263
static $session_handler = null;

0 commit comments

Comments
 (0)