Home > Programming, Servers & Scripts >

[PHP Script] URL Rotator (1)


12-11-2013 09:42 PM #1 swissfactor (Member)
[PHP Script] URL Rotator

Hey guys

I thought I would share my URL rotator script with you ;-)

First of all you create a file called class.rotator.php with the following content:

Code:
<?php
class Rotator
{
	private $urls;
	private $file;
	
	public function __construct($counter_file)
	{
		$this->urls = array();
		$this->file = $counter_file;
	}
	
	public function add_url($url)
	{
		$this->urls[] = trim($url);
	}
	
	public function number()
	{
		return count($this->urls)-1;
	}
	
	public function get_next($num)
	{
		if($num+1 > $this->number())
		{
			return 0;
		}
		else
		{
			return $num+1;
		}
	}
	
	public function run($simulate = false)
	{
		@$number = (int)file_get_contents($this->file);
		$next_index = $this->get_next($number);
		file_put_contents($this->file, $next_index);
		
		if($simulate)
		{
			echo $this->urls[$next_index];
		}
		else
		{
			header("Location: ".$this->urls[$next_index]);
		}
		exit;
	}
}
?>
(The above file is a url rotator library)

Then you can have your script like seniordatingoffer.php:
Code:
<?php
require 'class.rotator.php';

$s1 = $_GET['s1'];
$s2 = $_GET['s2'];
$s3 = $_GET['s3'];

$r = new Rotator('counter.txt');
$r->add_url('http://trkur.com/trk?o=6093&p=XXXXX&s1='.trim($s1).'&s2='.trim($s2).'&s3='.trim($s3));
$r->add_url('http://trkur.com/trk?o=7152&p=XXXX&s1='.trim($s1).'&s2='.trim($s2).'&s3='.trim($s3));
$r->run();
?>
The file above does equally rotate the visitors, ie. visitor 1 goes to the first url, visitor 2 goes to the second url, visitor 3 goes to the first url, etc.

I hope this helps someone ;-)


Home > Programming, Servers & Scripts >