PHP redirects

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
How from one home page would I redirect users equally towards 2 other pages?
Thanks.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Load balancing isn't really the purpose of php. You would best solve this problem with the web service (apache, nginx, etc) itself.
 

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
Load balancing isn't really the purpose of php. You would best solve this problem with the web service (apache, nginx, etc) itself.

Not load balancing,just a question in a test:do you have any idea?
Besides something half-assed like this:

if(rand(0,1))header( "Location:1" );
else header( "Location:2" );
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Without some kind of backend storage to track which location you used last, that's about how you can do it. You could spice it up with an array or something, but in the end you are still just going to pick one at random.

Of course assuming this requires no user interaction. If it required user interaction you would have two links on the page and call a different redirect for each link.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Not load balancing,just a question in a test:do you have any idea?
Besides something half-assed like this:

if(rand(0,1))header( "Location:1" );
else header( "Location:2" );

Did your test have any context on how or why you should choose between locations? If not load balancing then what is the purpose?

Without more context there is nothing better than using the random number generator.
 

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
'Create a PHP function that equally redirects the number of
visitors on a page to 2 other pages. All visitors on index.php
should be equally redirected: 50% on page1.php and 50% on
page2.php. Create this function without using MySQL databases'
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Well if you want true 50/50 split you are going to need a way to store what the last redirect was/is. Without a database your options are pretty limited.
 

Albatross

Platinum Member
Jul 17, 2001
2,344
8
81
Maybe something like this
for($i=1;$i<10000000;$i++)
if($i % 2 == 0)
header(location1)
elseif ($i % 2 != 0)
header(location2)

So every other user get the same page.

But i must store that $i somewhere,maybe in a file adn increment it from there?
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
Well if you want true 50/50 split you are going to need a way to store what the last redirect was/is. Without a database your options are pretty limited.

Not sure if PHP has something similar, but ASP.NET has a cache you could use. But the cache can be cleared which would leave you once again not knowing where the last redirect went.

Can you use any other form of persistence? If not, then I'm assuming this is really some sort of teaser that sees how closely you could get approx 50% of hits to one site or the other.

The sites you are redirecting to, can they persist information? So could you make a request to these sites that returns the number of times they have redirected to you? That adds quite a bit of latency though.

And this wouldn't necessarily be accurate if you redirected someone to a page while you had outstanding "count" requests out.

Otherwise, you might just do something simple like check datetime and redirect to #1 if the second is even or #2 if the second is odd.
 
Last edited:

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Over time the rand function should give you a 50/50 split even though in any short interval it won't be exactly 50/50.

Perhaps you should ask your instructor if there's any problem with using the rand?
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Over time the rand function should give you a 50/50 split even though in any short interval it won't be exactly 50/50.

Perhaps you should ask your instructor if there's any problem with using the rand?

If rand is giving you a 50/50 split consistently, it's not really random is it? (Yes I know that rand will most likely given a 50/50 in this situation.)

Honestly, if you want to be sure of a 50/50 there are few easy ways to do this and be fairly sure on even 50/50 split.

1) You could use memcache. Store a variable of true/false in the cache and on each page view just flip it and use that as your link criteria.
2) You could use a temp file. Write true or false to the file and check it on each page load. Use that to link to your criteria.
3) You could still use a temp file, but instead of writing to it, check for it's existence. If it exists, go to site 1 and delete it. If it doesn't, go to site two and create it.

However, technically it is possible to get two page loads at the same time, so a true 50/50 is still not possible with these methods. Honestly, this is just something that shouldn't be done with php.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Those seem more brittle than the rand since Apache (or whatever) will be running multiple threads.

But I agree, in the real world you'd want to use an external load balancer so Apache doesn't need to do all this work to choose a location / server.