Fastest way to get random php number?

Am I trying to prevent caching by adding '? t = 'to the end of my JS files. What is the fastest way to get such a number? time () or rand () or something else?

+3
source share
6 answers

time () and mt_rand () are quite similar in terms of efficiency in PHP - you choose one or the other based on what factors you need:

  • Just hard to guess: use mt_rand () (e.g. generating a usage salt)
  • Get a unique identifier that is hard to guess: use mt_rand (1, 931415926536); (e.g. generating a session id)
  • (obvious) save entries: use time () (e.g. prevent caching, logs, etc.)

, () , . ( .)

(mt_rand() 4 , rand())

, , , , ; , , , .

+6

, () .

+5

, - , , HTTP-, URL-. PHP:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

.htaccess apache ( -):

<FilesMatch "\.js$">
Header set Cache-Control "no-cache, must-revalidate"
Header set Expires "Sat, 26 Jul 1997 05:00:00 GMT"
</FilesMatch>
+3

Try:

'?t=' . mt_rand(time());
+2

rand(), mt_rand().

It uses a well-known random number generator using Mersenne Twister , which will generate random numbers four times faster than the average libc rand () provides.

+2
source
srand(time());
echo '?t=' . rand(); 
-1
source

Source: https://habr.com/ru/post/1751506/


All Articles