Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Simplifies cookie setting logic

AuthorDaveJarvis <email>
Date2023-11-09 00:53:44 GMT-0800
Commit957244989cd48c8a17c543213ca05235e497a80a
Parentd9d08a5
www/downloads/counter.php
/**
- * Answers whether the user's session has expired.
+ * Answers whether the user's download token has expired.
*
* @param int $lifetime Number of seconds the session lasts before expiring.
*
* @return bool True indicates the session has expired (or was not set).
*/
- function session_expired( $lifetime ) {
- // Session cookie, not used for user tracking, tracks last download date.
+ function download_token_expired( $lifetime ) {
$COOKIE_NAME = 'LAST_DOWNLOAD';
$now = time();
- $expired = !isset( $_COOKIE[ $COOKIE_NAME ] );
-
- if( !$expired && ($now - $_COOKIE[ $COOKIE_NAME ]) > $lifetime ) {
- unset( $_COOKIE[ $COOKIE_NAME ] );
- setcookie( $COOKIE_NAME, '', $now - 3600, '/' );
+ $expired = false;
+ if( !isset( $_COOKIE[ $COOKIE_NAME ] ) ) {
$expired = true;
+ setcookie( $COOKIE_NAME, $now, $now + $lifetime, '/' );
}
-
- // Update last activity timestamp.
- setcookie( $COOKIE_NAME, $now, $now + $lifetime );
return $expired;
*/
function hit_count( $filename ) {
- if( session_expired( 7 * 24 * 60 * 60 ) ) {
+ if( download_token_expired( 7 * 24 * 60 * 60 ) ) {
increment_count( $filename );
}
Delta6 lines added, 12 lines removed, 6-line decrease