Search code examples
phprandomnumbersweb-traffic

php: sending 10% of your traffic


My code:

$random_number = mt_rand(1,100);

if ($random_number <= 10){
    header('Location: http://www.site1.com');
} else {
    header('Location: http://www.site2.com');
}

exit;

Needless to say, it seems to work if you have a lot of traffic, but when you are down to around 1000 uniques a day, the percentage hovers around 35 to 40%.


Solution

  • For a more accurate distribution of traffic use a load balancer or increment a counter in your code.

    Here is a nice article on why mt_rand isn't so random