Jump to content

Help with PHP time() and Session

QQ_cazo

My code:

 

session_start();
$time = time() + 5;
if (isset($_SESSION['udi_v1']) && $time >= $_SESSION['udi_expire']) {
    # skip
    echo "NO SKIP";
} else {
    function guidv4($data = null) {
        $data = $data ?? random_bytes(16);
        assert(strlen($data) == 16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }
    $_SESSION['udi_v1'] = guidv4();
    $_SESSION['udi_expire'] = time();
    echo "NO SKIP________";
}
var_dump($_SESSION);

my goal: i want to make a new guidv4 every 15 minutes.

 

 

So if the udi_expire time is above 900 seconds, then refresh it

if the time is not the 900 seconds, then dont do anything

 

 

where did i go wrong?

Link to comment
Share on other sites

Link to post
Share on other sites

It looks to me like the way the code is written now, it will always go on the IF path, because your $time variable will always be 5 seconds bigger than what's stored in the session.

 

No idea, but I can tell you there's little reason to use vsprintf for such a function, you could use substr and concatenation ... it's not much performance difference but vsprintf is more heavy than a basic substr function

 

function guidv4($data = null) {
	$data = $data ?? random_bytes(16);
	assert(strlen($data) == 16);
	$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
	$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
	return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

function new_guidv4($data = null) {
	$r = $data ?? random_bytes(16);
	$r[6] = chr( ord($r[6]) & 0x0f | 0x40);
	$r[8] = chr( ord($r[8]) & 0x3f | 0x80);
	$output = bin2hex($r);
	$output = substr($output,0,8).'-'.substr($output,8,4).'-'.substr($output,12,4).'-'.substr($output,16,4).'-'.substr($output,20,12);
	return $output;
}

 

on 100k executions of each function the second function is almost 2x faster ... but considering how rarely it's run it doesn't really matter:

 

string(36) "cde32b47-0717-4521-8dfd-06ac5cfe0403"
0.17600989341736
string(36) "cde32b47-0717-4521-8dfd-06ac5cfe0403"
0.11600708961487

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, mariushm said:

It looks to me like the way the code is written now, it will always go on the IF path, because your $time variable will always be 5 seconds bigger than what's stored in the session.

 

No idea, but I can tell you there's little reason to use vsprintf for such a function, you could use substr and concatenation ... it's not much performance difference but vsprintf is more heavy than a basic substr function

 


function guidv4($data = null) {
	$data = $data ?? random_bytes(16);
	assert(strlen($data) == 16);
	$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
	$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
	return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

function new_guidv4($data = null) {
	$r = $data ?? random_bytes(16);
	$r[6] = chr( ord($r[6]) & 0x0f | 0x40);
	$r[8] = chr( ord($r[8]) & 0x3f | 0x80);
	$output = bin2hex($r);
	$output = substr($output,0,8).'-'.substr($output,8,4).'-'.substr($output,12,4).'-'.substr($output,16,4).'-'.substr($output,20,12);
	return $output;
}

 

on 100k executions of each function the second function is almost 2x faster ... but considering how rarely it's run it doesn't really matter:

 


string(36) "cde32b47-0717-4521-8dfd-06ac5cfe0403"
0.17600989341736
string(36) "cde32b47-0717-4521-8dfd-06ac5cfe0403"
0.11600708961487

 

 

well, im not worried about speed, but i need to make a new UUID every 15 mins based on the session, any way to do that?

Link to comment
Share on other sites

Link to post
Share on other sites

Can't you make a CRON job to execute a script on an interval?

Another option would be to use DateTime class and DateTime::diff method.https://www.php.net/manual/en/datetime.diff.php


I don't really seeing your idea working unless it's continuously checking time differences or something else is continuously provoking the check. Rarely there is a reason to actively check if something has expired. Rather issue an ID and an expiration date, then when the ID is being used check if expieration date has passed. This is quite trivial with DateTime and comparison operator(s).
 

<?php

$date_now = new DateTime();
$expiration_date = new DateTime('01.07.2021');

if ($expiration_date < $date_now) {
    echo 'It has expired!';
}

 

 

EDIT: Can also just store expiration timestamp in milliseconds (or seconds) and compare them and it will work. Your IF should check if expiration time is greater than current time, not if current time + 5 seconds is greater than expiration time.
 

Let's say it's 16:00 right now and our ID is set to expire at 16:15.

Your IF check will be "is 16:00:05 greater than 16:16:00?". It is not, so it will do the ELSE portion. Now if it actually HAS expired (current time is greater than expiration time), it will fall into the "SKIP" portion.

PS: Your code overall needs to be fixed:


 

<?php

if (!defined('UDI_V1_LIFE_CYCLE')) {
    define('UDI_V1_LIFE_CYCLE', 900);
}

// check if session is active, if not start a new session
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$time = time() + 5; // why add 5 seconds? Not really a mistake, so won't edit it

// I am not entirely sure what your idea over here is, but I am assuming if no udi_v1 is set, it should set one.
// Personally I might prefer empty() check, but depends on the design requirements
// If no udi_v1 or no expiration date or we're past the expiration date, set a new one
if (!isset($_SESSION['udi_v1']) || !isset($_SESSION['udi_expire']) || $_SESSION['udi_expire'] <= $time) {
    $_SESSION['udi_v1'] = guidv4();
    $_SESSION['udi_expire'] = $time + UDI_V1_LIFE_CYCLE;
    
    echo 'SET NEW VALUES!'; 
    die;                                                                                                
}
                                                                                                    
echo 'NO CHANGES!';
die;  
                                                                                                    
                                                                                                    
// absolutely no need to redfine this function every time                                                                                                
/**
 * enter your documentation here                                                                                                   
 */                                                                                                    
function guidv4($data = null) {
    $data = $data ?? random_bytes(16);
    assert(strlen($data) == 16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}                                                                                                    
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×