Hi, looked around but i couldnt find much on this. I need to redirect every 3 visitors that visit my url to different sites. so visitor 1 would go to xyz.com, lets say visitor 2 comes along and he gets redirect to xzy.com then the last visitor comes along and gets redirected to yxz.com and then the process repeats itself. My idea so far is to create a file in php with a number stored in it then incrememnt the number and use the modulus operator to see if it is divisible by 1, 2, or three and use the header function depending on what the number is at. im sure there is an easier way, im fairly new to php, so any examples would be very very appreciated. thank you in advance. Dave

I think what you propose will work, as long as you test if the number is divisible by 3, and then 2, in that order, as everything is divisible by 1.

I wonder, though, how well it would work under pressure? I mean, if there are several simultaneous visitors, will everything queue nicely?

Member Avatar for diafol

A DB solution may be better. Maybe not. Here's a trivial solution.

The php script has the following vars:

$sites = array("http://www.site1.com","http://www.site2.com","http://www.site3.com");
$max_sites = count($sites) - 1;
//get $num from DB value, e.g. 2
$current_site = $num + 1;
if($current_site > $max_sites)$current_site=0;
//update DB value to the $current_site 

echo "<a href=\"{$sites[$current_site]}\">{$sites[$current_site]}</a>";

Code will still work when you want to add additional sites. JUst add new urls to the array - nothing else to do.

Does it really need to rotate like that or can you just send 33% of visitors to site one, 33% to site two, and 33% to site three.

Because then you can just use php's RAND() function to randomly pick a number between 1 and 3, and depending upon which it chooses, it sends the user to the appropriate site. Super quick way of doing it without making it keep track of 'state'.

The random number generator, between 1 and 3, will average out to about 33% each. Of course, it's random, so not exact ;)

commented: Simple and non-intensive +7

Does it really need to rotate like that or can you just send 33% of visitors to site one, 33% to site two, and 33% to site three.

Because then you can just use php's RAND() function to randomly pick a number between 1 and 3, and depending upon which it chooses, it sends the user to the appropriate site. Super quick way of doing it without making it keep track of 'state'.

The random number generator, between 1 and 3, will average out to about 33% each. Of course, it's random, so not exact ;)

This is a vastly superior approach, as it will be quicker - no need to write to either a file or a DB, and it should not sink into a mire of locked files, etc, so is more likely to cope with high volumes.

Member Avatar for diafol

> This is a vastly superior approach

I agree. Much less intensive. It is also a scalable solution in the event that you need to add more sites.

Yes, brilliant :) Thanks queen of daniweb, i should have thought of that :)

Member Avatar for diafol

Just a note, you'd be better to use mt_rand().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.