diff options
| author | Connor Thomson <blumatrikz@gmail.com> | 2026-09-20 14:19:07 -0700 |
|---|---|---|
| committer | Connor Thomson <blumatrikz@gmail.com> | 2026-09-20 14:19:07 -0700 |
| commit | 7fae58283dff2b2f4f33c1b9c3554b7677f3f062 (patch) | |
| tree | 6bbcf93cb44f66801541f7789a9b0d07a8700c57 /php | |
| parent | 1177a625e825766c7727b254bc72718fd88e8145 (diff) | |
Switch to session ids
Diffstat (limited to 'php')
| -rwxr-xr-x | php/session.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/php/session.php b/php/session.php new file mode 100755 index 0000000..7895dfb --- /dev/null +++ b/php/session.php @@ -0,0 +1,50 @@ +<?php +/* + * GNUfault.org - GNUfault's website + * Copyright (C) 2026 Connor Thomson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +function start_session(): void { + if (session_status() === PHP_SESSION_ACTIVE) { + return; + } + + session_set_cookie_params([ + 'lifetime' => 0, + 'path' => '/', + 'secure' => true, + 'httponly' => true, + 'samesite' => 'Strict' + ]); + + session_start(); +} + +function set_user_id(int $user_id): void { + start_session(); + + session_regenerate_id(true); + + $_SESSION['user_id'] = $user_id; +} + +function get_user_id(): ?int { + start_session(); + + return isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null; +} + +?> |
