Skip to content

Only set session cookies on login - #1691

Merged
cpeel merged 1 commit into
DistributedProofreaders:masterfrom
cpeel:stingy-cookies
Oct 20, 2026
Merged

cpeel merged 1 commit into
DistributedProofreaders:masterfrom
cpeel:stingy-cookies

Conversation

@cpeel

@cpeel cpeel commented Oct 16, 2026

Copy link
Copy Markdown
Member

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.

No functional changes for authenticated users: users are still able to authenticate and get cookies and everything still works. Unauthenticated users no longer get cookies.

Compare:

curl -I https://www.pgdp.org/c/

curl -I https://www.pgdp.org/~cpeel/c.branch/stingy-cookies/

Sandbox: https://www.pgdp.org/~cpeel/c.branch/stingy-cookies

@cpeel
cpeel requested review from bpfoley and srjfoo October 16, 2026 22:25
@cpeel cpeel self-assigned this Oct 16, 2026
Comment thread pinc/dpsession.inc Outdated
_session_set_handlers_and_start();

// If it's a valid session, refresh the cookie and update user activity
if (isset($_SESSION['pguser']) && !empty($_SESSION['pguser'])) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the same trick here. (I'm mostly suggesting this because string literals are fragile. They're easy to mistype.

if (!empty($_SESSION['pguser'] ?? '')) { ... }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, thanks.

Comment thread pinc/dpsession.inc Outdated
// in this browsing session.
if (!isset($_COOKIE["http_referer"])) {
if (isset($_SERVER["HTTP_REFERER"])) {
dp_setcookie('http_referer', $_SERVER["HTTP_REFERER"]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify this to

dp_setcookie('http_referer', $_SERVER['HTTP_REFERER'] ?? '');

(and just to remind the reader that setting the cookie to the empty string clears it, you could add an 'otherwise clear it' to the comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided we shouldn't be sending a delete cookie everytime someone accesses a page without a referer header, so I've modified this code a bit differently.

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.
@cpeel
cpeel requested a review from bpfoley October 19, 2026 18:14
@cpeel
cpeel merged commit b9b066d into DistributedProofreaders:master Oct 20, 2026
12 checks passed
@cpeel
cpeel deleted the stingy-cookies branch October 20, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants