0

I have a link that goes to http://example.com/random.php, code for random.php below.

<?php
srand ((double) microtime( )*1000000);
$random_number = rand(1,100);
header( "Location: http://example.com/test?page=$random_number" ) ;
?>

Basically what I want it to do is link to a random page. It works initially, but after the first click it keeps linking back to the same supposedly random page every single time. Any idea how to fix this? or maybe a better way to approach the problem entirely?

8 Answers8

2

Either your browser or server is probably caching the page. Try this in your php code:

header("cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0");
davogones
  • 7,321
  • 31
  • 36
2

My guess would the web browser is caching the Location redirect. Try adding some "cache busting" headers to the top of the page

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

    srand ((double) microtime( )*1000000);
    $random_number = rand(1,100);
    header( "Location: http://example.com/test?page=$random_number" ) ;
?>

DISCLAIMER: If you're using this for nefarious purposes, a geek curse is hereby placed on you, and you will be eaten by a grue in short order.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • I can't think of any nefarious purpose to use random pages with. Can you elaborate please? – Spikolynn Jan 31 '09 at 12:21
  • From PHP Manual: "Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically." – Dawid Ohia Jan 22 '10 at 10:41
1

are you certain microtime( ) is supported on your OS, else you are re-seeding w/ the same value .. "This function is only available on operating systems that support the gettimeofday() system call"

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
1

As others have pointed out, it sounds like the browser is caching the redirect; there are various types of HTTP redirect, and depending upon the status code, the browser may or may not be allowed to cache the redirect.

You could try altering the response code to issue a 303 See Other, which is a type of redirect that user agents aren't supposed to cache. For example:

header( 'Location: http://www.example.com', true /* overwrite */, 303 );

For more information about HTTP redirect codes, take a look at the HTTP 1.1 specification; specifically section 10.3, which deals with redirection.

Rob
  • 47,999
  • 5
  • 74
  • 91
0

Have you tried this without the call to srand()? The PHP docs for srand say:

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

I wonder if it could be to do with your browser caching the redirect.

Ray Hidayat
  • 16,055
  • 4
  • 37
  • 43
0

If caching is becoming too much of a problem for you and you are unable to achieve via php, you can write a small javascript to do the same and be assured that you will get a random link every time.


function jump() {
var random = Math.floor(Math.random()*1000);

window.location="test?page="+random;
}

Alec Smart
  • 94,115
  • 39
  • 120
  • 184
-2

Um... your code works, bro; perhaps you've misunderstood the concept and think that reloading the page you've arrived at should take you to another randomly-generated URL...

Hexagon Theory
  • 43,627
  • 5
  • 26
  • 30