Jump to content

PHP link rotating script


hostsum

Recommended Posts

I have this PHP link rotating script, it works pretty fine...
when I run the script it redirect to:
link1.com
link2.com
link3.com
.... and so on

but when I change the web browser or someone else runs it, it restart from the begging..

I mean that it redirect back to link1.com

 

the script use a link.txt file where the link list is stored
what I want is that when I run the script it redirect to 
link1.com
when I change the web browser or someone else runs it continue redirecting to next link and so on...(to link2.com)

 

Here's the PHP code:

<?php
	
	function redirect_to($link) {
		header("Location: {$link}");
		exit;
	}
		
	$links = file('links.txt');	
	
	while (!$links) {
		$links = file('links.txt');	
	}
	
	$links_count = count($links);	
	$one_month = 60 * 60 * 24 * 30;
	

		$index = $_COOKIE['link_index'] + 1;		
		setcookie('link_index', $index, time() + $one_month);		
		
		if ($index == $links_count) {
			setcookie('link_index', 0, time() + $one_month);		
			$index = 0;
		}
	
	redirect_to($links[$index]);
	
?>

 Please help me solve this issue

 

Thanks

Link to comment
Share on other sites

Just for fun.  links.txt needs to be writable by the webserver:

$file = 'links.txt';
$links = file($file);

if(filemtime($file) <= strtotime('-1 month')) {
    array_push($links, array_shift($links));
    file_put_contents($file, $links, LOCK_EX);
} 
header('Location: ' . trim($links[0]));
exit;
Link to comment
Share on other sites

The problem is that you are using a COOKIE. Cookies are unique per computer and per browser, so when someone new gets on, or a new browser is used, their system has no 'link_index' cookie, and the count starts over. You need to save the 'link_index' on the server in some way that is not computer or browser specific. A Session won't work either, because it is also unique per computer and per browser, just stored on the server instead of the client machine.

Link to comment
Share on other sites

 

Just for fun.  links.txt needs to be writable by the webserver:

$file = 'links.txt';
$links = file($file);

if(filemtime($file) <= strtotime('-1 month')) {
    array_push($links, array_shift($links));
    file_put_contents($file, implode('', $links), LOCK_EX);
} 
header('Location: ' . trim($links[0]));
exit;

This solution should work for what you want, because it rotates the links in the file itself, rather than keeping a counter.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.