Welcome Guest, Not a member yet? Register   Sign In
Method always returns the same value (cache?)
#1

(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 $lengthbool $onlyLetters falsebool $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 truebool $separated false): string
 
{
 
mt_srand((int)microtime()*10000);
 
$charid md5(uniqid(rand(), true));
 
$hyphen $separated chr(45) : ''// chr(45) = "-"
 
$guid substr($charid08).$hyphen
 
.substr($charid84).$hyphen
 
.substr($charid,124).$hyphen
 
.substr($charid,164).$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?
Reply
#2

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 )
Reply
#3

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 $lengthbool $onlyLetters falsebool $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);

Reply
#4

(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 $lengthbool $onlyLetters falsebool $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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB