2012-07-31 34 views
-1

如何在PHP文件中仅向20%的访问者展示广告?或者向另外80%的访问者展示不同的广告?仅向20%的访问者展示广告。或向另一个80%的访问者提供不同的广告

任何可以工作的方式,可能会使用时间间隔,例如仅在第0-12分钟访问该网站的访问者仅显示该访问者,而在第13-59分钟访问该网站的访问者不是或者其他方法可以工作。

请帮助或分享,如果你有,这应该是很容易的,很多人需要它,已经在Google上搜寻,但仍然使用随机数生成器不能找到它

回答

1

一种可能,简单的解决方案:

$randInteger = rand(1, 10); // Generate a random number in variable $randInteger. 
if ($randInteger <= 2) // If the integer is 1 or 2, show the 20% ad. 
{ 
    showTwentyPercentAd(); 
} 
else if ($randInteger >= 3) // Otherwise, if the integer is 3-10, show the 80% ad. 
{ 
    showEightyPercentAd(); 
} 

希望这很清楚代码的结构。

+2

比较'$ randInteger> = 3'是多余的。 – 2012-07-31 08:16:01

+0

@ChrisOrtner是的,我知道,我希望原始提问者明白到底发生了什么。可以说代码评论也不是必需的,但我们都在这里学习,我想写一些更清楚的解释。 – Stegrex 2012-07-31 08:16:49

6

我会简单地用一个随机数去:

if (rand(0, 100) <= 20) 
{ 
    ShowAd(1); 
} else { 
    ShowAd(2); 
} 
2

努力让搜索?

$adPercent = 20; 
if (rand(0, 100) < $adPercent) { 
    echo '<div class="ads">Buy now!</div>'; 
} 

得到这个从这里:

Display Ads To % of Users