3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class CsrfToken { private $value; public function __construct() { $this->value = bin2hex(random_bytes(32)); $this->createdTime = time(); } public function getCreatedTime(): int { return $this->createdTime; } public function equals($other) { return hash_equals($this->value, (string)$other); } public function __toString() { return $this->value; } } interface CsrfTokenStore { function getCsrfToken(bool $invalidate = false): CsrfToken; } class Session implements CsrfTokenStore { private $csrfTokenLifetime; public function __construct($csrfTokenLifetime = 300) // your basic session wrapper class { $this->csrfTokenLifetime = $csrfTokenLifetime; session_start(); } private function isTokenValid(?CsrfToken $token): bool { return $token instanceof CsrfToken && time() < $token->getCreatedTime() + $this->csrfTokenLifetime; } public function getCsrfToken(bool $invalidate = false): CsrfToken { if (!$this->isTokenValid($_SESSION['csrf_token'] ?? null)) { $_SESSION['csrf_token'] = new CsrfToken(); } $token = $_SESSION['csrf_token']; if ($invalidate) { unset($_SESSION['csrf_token']); } return $token; } } /* to output the token, pass a CsrfTokenStore to the template: <input name="csrftoken" type="hidden" id="csrf_token" value="<?= $tokenStore->getCsrfToken(); ?>"> to verify the token, pass a CsrfTokenStore to the controller - clear the token and generate a new one for this page load if (!$tokenStore->getCsrfToken(true)->equals($_GET['csrf_token'])) throw new Exception(); */

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.3.10.0050.01116.42
7.3.00.0060.00716.61
7.2.130.0030.01016.77
7.2.120.0050.00916.63
7.2.110.0080.00516.48
7.2.100.0060.00816.58
7.2.90.0090.00616.45
7.2.80.0060.00916.60
7.2.70.0050.00916.66
7.2.60.0060.00816.70
7.2.50.0050.00916.64
7.2.40.0090.00616.62
7.2.30.0080.00716.68
7.2.20.0040.00916.66
7.2.10.0100.00316.71
7.2.00.0070.00616.62
7.1.250.0060.00815.57
7.1.230.0100.00615.40
7.1.220.0000.01315.29
7.1.210.0100.00315.29
7.1.200.0080.00415.61
7.1.190.0100.00315.35
7.1.180.0060.00915.43
7.1.170.0030.01015.54
7.1.160.0110.00315.51
7.1.150.0070.00415.48
7.1.140.0090.00315.57
7.1.130.0060.00815.46
7.1.120.0090.00315.36
7.1.110.0000.01215.24
7.1.100.0060.00615.57
7.1.90.0070.00415.39
7.1.80.0050.00515.55
7.1.70.0110.00315.50
7.1.60.0130.01633.45
7.1.50.0270.00733.14
7.1.40.0260.01333.25
7.1.30.0190.01333.46
7.1.20.0210.02033.41
7.1.10.0140.00015.60
7.1.00.0070.00915.25
5.6.380.0100.00713.50

preferences:
43.02 ms | 660 KiB | 5 Q