diff options
| author | Syndamia <kamen@syndamia.com> | 2025-01-25 19:58:27 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2025-01-25 19:59:25 +0200 |
| commit | decc7e47146584dcc02171793072462ce0f817fe (patch) | |
| tree | 5851b92c5f2abdcb85445e05fff7fa19467975a6 | |
| parent | f7db20f6a635a4195647941a8c150a1bb3fcf904 (diff) | |
| download | nowayforward_human-decc7e47146584dcc02171793072462ce0f817fe.tar nowayforward_human-decc7e47146584dcc02171793072462ce0f817fe.tar.gz nowayforward_human-decc7e47146584dcc02171793072462ce0f817fe.zip | |
feat(models/cookies): Added model with proper logic for cookie creation
| -rw-r--r-- | migrations/00-initial.sql | 2 | ||||
| -rw-r--r-- | models/cookies.php | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/migrations/00-initial.sql b/migrations/00-initial.sql index 7b08719..879b5f1 100644 --- a/migrations/00-initial.sql +++ b/migrations/00-initial.sql @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS Users ( CREATE TABLE IF NOT EXISTS Cookies ( UID INT NOT NULL AUTO_INCREMENT, - Token CHAR(32) NOT NULL, + Token CHAR(36) NOT NULL, Expires DATETIME, PRIMARY KEY (UID, Token), FOREIGN KEY (UID) REFERENCES Users(UID) diff --git a/models/cookies.php b/models/cookies.php new file mode 100644 index 0000000..8a7ea42 --- /dev/null +++ b/models/cookies.php @@ -0,0 +1,30 @@ +<?php +namespace Database; +use PDO; + +class Cookie extends Table { + public $UID; + public $Token; + public $Expires; + + static function create(string $UID) : string { + $Token = uuidv4(); + Table::_create( + "Cookies", + "(UID, Token, Expires)", + "(\"$UID\", \"$Token\", NOW() + INTERVAL 30 DAY)", + ); + return $Token; + } +} + +// Taken from https://stackoverflow.com/a/15875555 +function uuidv4() +{ + $data = random_bytes(16); + + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 + + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); +} |
