tags:

views:

547

answers:

8

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?

A: 

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
A: 

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
A: 

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

Ray Hidayat
+2  A: 

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
+2  A: 

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.

Alan Storm
I can't think of any nefarious purpose to use random pages with. Can you elaborate please?
Spikolynn
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."
JohnM2
Was just copying the original poster's code.
Alan Storm
+1  A: 

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
A: 

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
+1  A: 

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