added unique but sortable time
All checks were successful
Build and push / Pulling repo on server (push) Successful in 19s

This commit is contained in:
Chris 2023-10-21 22:12:56 +02:00
parent b095073447
commit fc9de5b15c
3 changed files with 50 additions and 17 deletions

View File

@ -104,6 +104,7 @@ class Page
//render template by running include //render template by running include
ob_start(); ob_start();
if(is_array($this->variables))
extract($this->variables); extract($this->variables);
if($templatefile!= '') if($templatefile!= '')
include($templatefile); include($templatefile);

View File

@ -147,26 +147,49 @@ function getHighestSQLVersion()
return end($files); return end($files);
} }
function gen_uuid() { function gen_ulid($milliseconds = null,$lowercase = false) {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', if($milliseconds===null)
// 32 bits for "time_low" $milliseconds = (int) (microtime(true) * 1000);
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), $encodingChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
$encodingLength = 32;
$randomLength = 16;
$lastRandChars = [];
$timeLength = 10;
// 16 bits for "time_mid" $timeChars = '';
mt_rand( 0, 0xffff ), $randChars = '';
// 16 bits for "time_hi_and_version", for ($i = $timeLength - 1; $i >= 0; $i--) {
// four most significant bits holds version number 4 $mod = $milliseconds % $encodingLength;
mt_rand( 0, 0x0fff ) | 0x4000, $timeChars = $encodingChars[$mod].$timeChars;
$milliseconds = ($milliseconds - $mod) / $encodingLength;
}
// 16 bits, 8 bits for "clk_seq_hi_res", for ($i = 0; $i < $randomLength; $i++) {
// 8 bits for "clk_seq_low", $lastRandChars[$i] = random_int(0, 31);
// two most significant bits holds zero and one for variant DCE1.1 }
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node" for ($i = 0; $i < $randomLength; $i++) {
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) $randChars .= $encodingChars[$lastRandChars[$i]];
); }
return ($value = $timeChars . $randChars) && $lowercase ? strtolower($value) : strtoupper($value);
}
function ulid_to_timestamp($ulid)
{
$encodingChars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
$encodingLength = 32;
$timeLength = 10;
$timeChars = substr($ulid,0,$timeLength);
$time = 0;
for ($i = 0; $i < $timeLength; $i++) {
$time = $time * $encodingLength + strpos($encodingChars, $timeChars[$i]);
}
return $time;
} }
function escape($str) function escape($str)

View File

@ -9,6 +9,15 @@ class Login extends Page {
//return print_r($_REQUEST, true); //return print_r($_REQUEST, true);
} }
function test()
{
return nl2br(print_r([
'uuid' => gen_ulid(),
'timestamp' => ulid_to_timestamp("01HD9XN98F8SGT01X527KBNHRN"),
'freshtime' => ulid_to_timestamp(gen_ulid())
],true));
}
function validate() function validate()
{ {
$email = $_REQUEST['email']; $email = $_REQUEST['email'];