Jump to content

Sequential URL Rotator


dombrorj

Recommended Posts

I'm trying to set up a script that will rotate the URLs a user visits when they click a link. When they "Click Here" I'd like it to direct them to 1 of 5 urls in the list. This should be sequential, so if 100 people click the link, each of the 5 links should be visited 20 times total.

 

In other words, it can't randomly select the URL to direct the user. It should be evenly distributed.

 

Here's what I have so far, but is not working...

 

 

ROTATE.PHP

<?php
$linksfile ="links.txt";
$posfile = "pos.txt";

$links = file($linksfile);
$numlinks = count($linksfile);

$fp = fopen($posfile, 'r+') or die("Failed to open posfile");
flock($fp, LOCK_EX);
$num = fread($fp, 1024);
if($num<$numlinks-1) {
fwrite($fp, $num+1);
} else {
fwrite($fp, 0);
}
flock($fp, LOCK_UN);
fclose($fp);

header("Location: {$links[$num]}");
?>

 

LINK.TXT

http://www.mysite.com/link1.html
http://www.mysite.com/link2.html
http://www.mysite.com/link3.html
http://www.mysite.com/link4.html
http://www.mysite.com/link5.html

 

POS.TXT

(blank txt file)

 

Thanks in advance!

Link to comment
Share on other sites

You're making it a bit more complicated than it really is. You have the links in an array so work with the array to do as you need.


$linksfile ="./links.txt";
$links = file("$linksfile");

$use_this=array_shift($links);//take a line to use from the top

array_push($links,$use_this);// puts the line back on the bottom

trim("$use_this");//remove new line character from end of line

file_put_contents("$linksfile","");//empty the links file

foreach($links as $link)//loop through the links array
{
file_put_contents("$linksfile","$link",FILE_APPEND);//append the links to the linksfile
}
echo "<a href=\"$use_this\">$use_this</a>"; //echo the link

 

Thats the simple of it.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Thanks teamatomic...

 

Edit:

 

That's making progress. I have 5 url in the array. With your script above, it outputs the first and 5th url together, like:

 

http://mysite.com/link1.htmlhttp://mysite.com/link2.html

 

Also, the script needs to be a redirect script rather than echoing the URL. So the user will click "redirect.php" and it will take them to 1 of the 5 URLS. Does that make sense?

 

Thanks again

Link to comment
Share on other sites

I think this code will work, but for some reason it gives me a blank page and enters 0s in pos.txt, even though there are 5 urls listed in links.txt. It doesn't redirect when executed. What's wrong here?

 

<?php
$linksfile ="links.txt";
$posfile = "pos.txt";

$links = file($linksfile);
$numlinks = count($linksfile);

$fp = fopen($posfile, 'r+') or die("Failed to open posfile");
flock($fp, LOCK_EX);
$num = fread($fp, 1024);
if($num<$numlinks-1) {
fwrite($fp, $num+1);
} else {
fwrite($fp, 0);
}
flock($fp, LOCK_UN);
fclose($fp);

header("Location: {$links[$num]}");
?>

Link to comment
Share on other sites

There is nothing wrong with the code I gave you. The problem with the stacking of the lines is line 5. Thats because you built the file in a editor and did not hit enter after adding line 5. Therefore there is no line ending on line 5 creating in essence a broken file as far as internet use is concerned.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Thanks, that solved that. I'm a designer and working with extremely limited coding skills here so really appreciate the help here. Couple questions...

 

How can I get the scrip to do a header redirect rather than echo?

 

Is your script going to start with link 1 for each unique visitor? Or, if person A visits the site and clicks, they'll go to link 1... then person B from a different ip clicks and goes to link 2. I'm trying to get the later to work.

 

The reason I was using the code above was because of some concerns with volume of traffic. This page receives thousands of visitors an hour, and a previous script I was using was putting too much load on the server. The other issue was when multiple people were visiting and simultaneously clicking the link, it would fail.

Link to comment
Share on other sites

The code takes the top link and uses it in the url and then puts it on the bottom so it rotates links each time it is called.

 

If you want it to redirect then replace the echo with a header(location).

 

If you expect lots of traffic its OK as FILE_APPEND and LOCK_EX are mutually exclusive, there will be no failure, just slight lag as the file is being written to. But it would be no different than writing to the pos file, only a few milliseconds more.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

  • 1 month later...

This is still working for me, but I'd like to append a variable from the URL to the end of the URLs the links.txt file.

 

So, my link pointing to rotator.php is: mysite.com/rotator.php?variable=abc

 

I need to get "abc" and append it to the end of the URLs in link.txt

 

http://www.mysite.com/link1.html?variable=
http://www.mysite.com/link2.html?variable=

 

In the rotator.php file, I added

$variable = $_GET['variable'];

 

But I can't seem to echo $variable in the script above without breaking. Any way to do this?

 

Thanks!

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.