加权网站旋转器


Weighted website rotator

我需要为我创建的所有链接提供一个加权旋转器,以便流量可以按加权比例分布到我定义的目的地。我目前使用以下内容:

<?
header('Location: www.destinationwebsite1.com/index.php');
?>

然而,这仅将流量分配到1个源。。我想要一些基于"重量"的东西,无论我定义了多少目的地。。

例如:

25% to www.destinationwebsite1.com/index.php 
25% to www.destinationwebsite2.com/index.php 
25% to www.destinationwebsite3.com/index.php 
25% to www.destinationwebsite4.com/index.php 

或者我选择的任何百分比。有人有什么想法吗?

最佳-N

使用rand om编号,并根据该编号将结果发送到其他位置。

// equal weights:
$sites = Array(
     "http://www.example.com/",
     "http://google.com/",
     "http://youtube.com/"
);
header("Location: ".$sites[rand(0,count($sites)-1)]);
// individual weights:
$sites = Array(
     "http://www.example.com/" => 50,
     "http://google.com/" => 30,
     "http://youtube.com/" => 20
);
$rand = rand(0,array_sum($sites)-1);
foreach($sites as $site=>$weight) {
    $rand -= $weight;
    if( $rand < 0) break;
}
header("Location: ".$site);

类似的东西?

<?php
$r = rand() / getrandmax();
if ( $r < 0.25 )
{
    header( 'Location: www.destinationwebsite1.com/index.php' );
}
elseif ( $r < 0.50 )
{
    header( 'Location: www.destinationwebsite2.com/index.php' );
}
elseif ( $r < 0.75 )
{
    header( 'Location: www.destinationwebsite3.com/index.php' );
}
else
{
    header( 'Location: www.destinationwebsite4.com/index.php' );
}
?>

从统计数据来看,这应该会将25%的访问者发送到每个网站。要更改每个网站的访问者比例,只需更改权重即可。