Note that there are some explanatory texts on larger screens.

plurals
  1. POWorkaround for suhosin.mt_srand.ignore to consistently shuffle an array in PHP?
    primarykey
    data
    text
    <p>I have a PHP script which needs to randomise an array with consistent results, so it can present the first few items to the user and they can then pull in more results from the same shuffled set if they want to.</p> <p>What I'm currently using is this (based on the Fisher Yates algorithm I believe):</p> <pre><code>function shuffle(&amp;$array, $seed) { mt_srand($seed); for ($a=count($array)-1; $a&gt;0; $a--) { $b = mt_rand(0, $a); $temp = $array[$a]; $array[$a] = $array[$b]; $array[$b] = $temp; } } </code></pre> <p>Which works fine on my local installation, but the server it needs to run on has Suhosin installed, which overrides mt_srand, meaning the seed is ignored, the array is just randomly shuffled and the user gets duplicate results.</p> <p>Everything I've found on Google suggests I need to disable suhosin.mt_srand.ignore (and suhosin.srand.ignore, not sure if the latter is relevant though) so I put the following in .htaccess:</p> <pre><code>php_flag suhosin.mt_srand.ignore Off php_flag suhosin.srand.ignore Off </code></pre> <p>I have no access to php.ini on this server so AFAIK that's the only way I can do it. The problem is that has no effect - phpinfo() still shows both settings as On, whereas I can change other Suhosin settings using .htaccess no problem.</p> <p>So I suppose what I'm looking for is either a way to actually disable suhosin.mt_srand.ignore (or a reason why it isn't working), or a workaround to seed a random number generator from within PHP. Or will I just have to implement another RNG myself?</p> <p>Any help would be much appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload