相关文章推荐
近视的镜子  ·  LDAP安装步骤开发者社区·  7 月前    · 
近视的镜子  ·  Method always returns ...·  7 月前    · 
近视的镜子  ·  Python搞定表格可视化!·  7 月前    · 
近视的镜子  ·  Control.ControlCollect ...·  10 月前    · 
近视的镜子  ·  Qt - XML和JSON - [BORUTO] ·  11 月前    · 
文雅的沙滩裤  ·  WKWebView拦截请求资源 ·  1小时前    · 
独立的眼镜  ·  如何连接Babelfish for RDS ...·  2 小时前    · 
发财的蛋挞  ·  Microsoft Azure Data ...·  2 小时前    · 
冷冷的投影仪  ·  Secure an ASP.NET ...·  3 小时前    · 
不羁的生姜  ·  PSPSDK 开发的时候出现 ...·  3 小时前    · 
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? 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? W hat did you Expect?
Joined CodeIgniter Community 2009.  ( Skype: insitfx )
$ php -v
PHP 8.0.15 (cli) (built: Jan 20 2022 04:25:44) ( NTS )
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 );
}
ci-phpunit-test | CodeIgniter Testing Guide | CodeIgniter 3 to 4 Upgrade Helper $ php -v
PHP 8.0.15 (cli) (built: Jan 20 2022 04:25:44) ( NTS )
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.
 
推荐文章