-
RedskyThirty Junior Member
 
-
Posts: 19
Threads: 10
Joined: Nov 2019
Reputation:
1
03-23-2022, 01:25 PM
(This post was last modified: 03-23-2022, 02:56 PM by RedskyThirty.)
Hello,
I had a big mistake today that I was not able to solve.
I have a method to generate a random string:
PHP Code: function RS_RANDOM_STRING(int $length, bool $onlyLetters = false, bool $onlyNumbers = false): string { $chars = ''; if (!$onlyNumbers || $onlyLetters) $chars .= 'abcdefghijklmnopqrstuvwxyz'; if (!$onlyLetters || $onlyNumbers) $chars .= '0123456789'; $numChars = strlen($chars); $loop = ceil($length / $numChars); return substr(str_shuffle(str_repeat($chars, $loop)), 0, $length); }
Sometimes, when this method is called, it always returns the same value as if it has been cached.
Another strange thing is that if I call the following method instead of the previous one, I get a new value at each call so no more "cache" mistake.
PHP Code: function RS_GENERATE_GUID(bool $lowercase = true, bool $separated = false): string { mt_srand((int)microtime()*10000); $charid = md5(uniqid(rand(), true)); $hyphen = $separated ? chr(45) : ''; // chr(45) = "-" $guid = substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12); return $lowercase ? $guid : strtoupper($guid); }
I'm pretty sure I didn't have this problem with older versions of the framework and when I was in PHP 7.4 (now 8.1.2).
Does anyone understand why the first method seems to be cached sometimes and always return the same value and the second one always return a new value?
-
InsiteFX Super Moderator
     
-
Posts: 6,720
Threads: 343
Joined: Oct 2014
Reputation:
246
This is in a help that I use and works great.
PHP Code: /** * guidV4 () * ----------------------------------------------------------------------- * * A universally unique identifier (UUID) it is a 128-bit number * used to identify information in computer systems. * * xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx * */ if ( ! function_exists('guidV4')) { /** * guidV4 () * ------------------------------------------------------------------- * * @return string */ function guidV4() { // Microsoft guid {xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx} if (function_exists('com_create_guid') === true) { return trim(com_create_guid(), '{}'); }
$data = openssl_random_pseudo_bytes(16);
// set version to 0100 $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// set bits 6-7 to 10 $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } }
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
Can't reproduce.
Code: $ php public/index.php
almjv976uc
$ php public/index.php
o51xgm4l6e
$ php public/index.php
igcjb9h8kt
$ php -v
PHP 8.0.15 (cli) (built: Jan 20 2022 04:25:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.15, Copyright (c) Zend Technologies
with Xdebug v3.1.1, Copyright (c) 2002-2021, by Derick Rethans
with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
PHP Code: <?php
namespace App\Controllers;
class Home extends BaseController { public function index() { echo RS_RANDOM_STRING(10) . PHP_EOL; } }
function RS_RANDOM_STRING(int $length, bool $onlyLetters = false, bool $onlyNumbers = false): string { $chars = ''; if (!$onlyNumbers || $onlyLetters) $chars .= 'abcdefghijklmnopqrstuvwxyz'; if (!$onlyLetters || $onlyNumbers) $chars .= '0123456789';
$numChars = strlen($chars); $loop = ceil($length / $numChars);
return substr(str_shuffle(str_repeat($chars, $loop)), 0, $length); }
-
RedskyThirty Junior Member
 
-
Posts: 19
Threads: 10
Joined: Nov 2019
Reputation:
1
(03-25-2022, 04:27 AM)kenjis Wrote: Can't reproduce.
Code: $ php public/index.php
almjv976uc
$ php public/index.php
o51xgm4l6e
$ php public/index.php
igcjb9h8kt
$ php -v
PHP 8.0.15 (cli) (built: Jan 20 2022 04:25:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.15, Copyright (c) Zend Technologies
with Xdebug v3.1.1, Copyright (c) 2002-2021, by Derick Rethans
with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
PHP Code: <?php
namespace App\Controllers;
class Home extends BaseController { public function index() { echo RS_RANDOM_STRING(10) . PHP_EOL; } }
function RS_RANDOM_STRING(int $length, bool $onlyLetters = false, bool $onlyNumbers = false): string { $chars = ''; if (!$onlyNumbers || $onlyLetters) $chars .= 'abcdefghijklmnopqrstuvwxyz'; if (!$onlyLetters || $onlyNumbers) $chars .= '0123456789';
$numChars = strlen($chars); $loop = ceil($length / $numChars);
return substr(str_shuffle(str_repeat($chars, $loop)), 0, $length); }
I will try to upload a zip file containing the project where this issue is reproducible.
|