0) { // Check for and authorize from persistent cookie if (isset($_COOKIE['persistentlogin'])) { if (hash_equals($pwhash, $_COOKIE['persistentlogin'])) { $auth = true; // Refresh cookie with new expiry // setcookie( $name, $value, $expire, $path, $domain, $secure, $httponly ) setcookie('persistentlogin', $pwhash, time() + 60 * 60 * 24 * 7, null, null, null, true); } else { // Invalid cookie $auth = false; setcookie('persistentlogin', '', 1); } } // Compare doubly hashes password input with saved hash elseif (isset($_POST['pw'])) { $postinput = hash('sha256', hash('sha256', $_POST['pw'])); if (hash_equals($pwhash, $postinput)) { // Regenerate session ID to prevent session fixation session_regenerate_id(); // Clear the old session $_SESSION = array(); // Set hash in new session $_SESSION['hash'] = $pwhash; // Set persistent cookie if selected if (isset($_POST['persistentlogin'])) { // setcookie( $name, $value, $expire, $path, $domain, $secure, $httponly ) setcookie('persistentlogin', $pwhash, time() + 60 * 60 * 24 * 7, null, null, null, true); } // Login successful, redirect the user to the homepage to discard the POST request if ('POST' === $_SERVER['REQUEST_METHOD'] && 'login' === $_SERVER['QUERY_STRING']) { header('Location: index.php'); exit; } $auth = true; } else { $wrongpassword = true; } } // Compare auth hash with saved hash elseif (isset($_SESSION['hash'])) { if (hash_equals($pwhash, $_SESSION['hash'])) { $auth = true; } } // API can use the hash to get data without logging in via plain-text password elseif (isset($api, $_GET['auth'])) { if (hash_equals($pwhash, $_GET['auth'])) { $auth = true; } } else { // Password or hash wrong $auth = false; } } else { // No password set $auth = true; }