PHP script to rotate through aff links

Status
Not open for further replies.

lucab

New member
Jan 4, 2007
677
3
0
I am trying to write a script that rotates through affiliate links. For example, person A clicks on the sign up link, and gets sent to aff link A. Person B comes by and clicks on the same sign up link, but gets sent to aff link B.

I really don't know shit about PHP, so I am just sort of hacking my way through this. The problem I am having is that I need to define my variables, but then they kept getting reset when the page reloads. Here is what I have so far:

PHP:
<?php
$aff_count = "1";
$aff_link = "http://afflink.com";

function checkAff(){
                 switch ($aff_count){
                  case "1":
                     $aff_link = "http://afflink.com?a=1";
                     break;
                    case "2":
                     $aff_link = "http://afflink.com?a=2";
                     break;
                    case "3":
                     $aff_link = "http://afflink.com?a=3";
                      break;
                    case "4":
                      $aff_link = "http://afflink.com?a=4";
                     break;
               $aff_count += 1;
}
print '<a href="'.$aff_link.'" >click here and buy stuff!</a>';
}
?>

Is this heading in the right direction? I don't think I can define the variables on the page that I have the link on... so I have to make another php file that is used as a reference point. But how do I prevent the variables from being reset every time it is referenced?

Any help would be greatly appreciated. Thank you,

lucab
 


You have to store them outside of php. So either a file, database record, or something like memcache, or you could just use a random number generator and redirect based on the output. Over the long term that will give you an even distribution though it's not straight up rotating the links.
 
  • Like
Reactions: lucab
I haven't tried this, but give it a whirl, just make sure to create the file called count.txt
PHP:
<?php

function randomizeAff() {
	$line = file('count.txt');
	$links = array(1 => 'http://afflink.com?a=1', 
								 2 => 'http://afflink.com?a=2', 
								 3 => 'http://afflink.com?a=3', 
								 4 => 'http://afflink.com?a=4');
	$affLink = $links[$line[0]];
	$fp = fopen('count.txt', 'w');
	$line = $line[0] + 1;
	$line = ($line > 4) ? 1 : $line;
	fwrite($fp, $line);
	fclose($fp);
	return $affLink;
}

$aff_link = randomizeAff();
print '<a href="'.$aff_link.'" >click here and buy stuff!</a>';

?>
 
  • Like
Reactions: lucab
Lemme step in here and recommend GoTryThis again.

You can read my review of it (link in sig) but basically one feature is a built in split tester for exactly what you're trying to do.

Plus it does a lot of other cool things.

Would you rather be bangin code together or actually getting your aff links out there?
 
I am trying to write a script that rotates through affiliate links. For example, person A clicks on the sign up link, and gets sent to aff link A. Person B comes by and clicks on the same sign up link, but gets sent to aff link B.

How is this useful?
 
Seems like this has quite a bit of demand, I might put more effort into making my scripts usable by the general public. I'll keep you guys posted and try to release an early version by the end of this weekend.
 
Status
Not open for further replies.