PHP Simple Splitter - Even Rotation

Mike

New member
Jun 27, 2006
6,779
114
0
50
On the firing line
If you do any sort of landing page testing, you know the value of a good splitter. Here is mine. With it you can get even rotation regardless of the number of pages you want to split. Just change a couple of items, and have at it.

If you want to rotate a lot of things, then you're better off re-writing this to use an array and loop it.

But for 2 - 4 pages, this is pretty idiot proof.


For an A / B split:
Code:
            $file = 'count.txt';  // file where the number is stored
            $num = file_get_contents($file); // variable to store number from file
            
            
                if ($num == 1) {
                        header( 'Location: http://website1.com/lp1.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num++;    // increment number by one    

                } else {
                        $num = $num-1; // decrease number by number of tests minus 1. Ex: 3 tests = decrease by 2
            
                }
            
            
            file_put_contents($file, $num); // write new number to file
For A / B / C split
Code:
            $file = 'count.txt';  // file where the number is stored
            $num = file_get_contents($file); // variable to store number from file
            
            
                if ($num == 1) {
                        header( 'Location: http://website1.com/lp1.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num++;    // increment number by one    
                } elseif ($num == 2) {
                        header( 'Location: http://website1.com/lp2.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num++;    // increment number by one    
                } else {
                        $num = $num-2; // decrease number by number of tests minus 1. Ex: 3 tests = decrease by 2
            
                }
            
            
            file_put_contents($file, $num); // write new number to file
You may notice that the else statement in these examples only decreases the count and doesn't redirect. That is because I usually put this on one of the pages I am testing (above the HTML). If you want to put this script on a splitter page (ie. split.php), then simply add a redirect to the else statement, like this:

Code:
          $file = 'count.txt';  // file where the number is stored
            $num = file_get_contents($file); // variable to store number from file
            
            
                if ($num == 1) {
                        header( 'Location: http://website1.com/lp1.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num++;    // increment number by one    
                } elseif ($num == 2) {
                        header( 'Location: http://website1.com/lp2.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num++;    // increment number by one    
                } else {
                        header( 'Location: http://website1.com/lp3.php?'.$_SERVER['QUERY_STRING'] ) ;
                        $num = $num-2; // decrease number by number of tests minus 1. Ex: 3 tests = decrease by 2
            
                }
            
            
            file_put_contents($file, $num); // write new number to file
Also, since the php header redirect is not relative, you can also test between domains.

Enjoy!
 


This is not atomic, if that matters, consider using redis's incr command and then get the modulus of how many items you have.

Or do what I do, and pick a random link each time. Over a decent number of clicks, it's close enough to even.

PHP for the redis thing:

Code:
$urls = array('a', 'b', 'c');
$url = $urls[$redis->incr('counter') / count($urls)];
Didn't test, haven't written PHP in about 2 years, good luck.

Edit: Mike, even with your files solution, why not just increment and take the modulus? It keeps the code simpler. In your posted code, you only ever direct to n-1 urls, where n is the number you actually want.
 
As above, writing a file like this isn't going to work. It could be edited inbetween as mattseh said because it's not atomic.
 
Just incase anyone wants the simple version:

<?
// Read from file/db whatever.
$urls = ['http://gayseks.com', 'http://wealllose.co.uk'];
$len = count($urls);
// Redirect here if wanted.
echo $urls[mt_rand(0, $len) % $len];
?>
 
Thanks for the feedback guys!

Why didn't I do it a different way? Simple: I'm a marketer first, and coder-wannabe second. So, I code in what is easiest for me, knowing there are smarter coders here that are more than happy to pick apart my chicken scratch and improve upon it. :D

Seriously though, I appreciate the help. Love learning better ways to do things.