URL Rotator Script Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arielpv1
Forum Newbie
Posts: 12
Joined: Mon Apr 21, 2008 7:57 pm

URL Rotator Script Help

Post by arielpv1 »

Hi,

I have a short php code that rotates urls.

I have 2 files.

This is the script:
1=> urlrotator.php

THis is where I enter the URLs. One in every line.
2=> urlrotator.txt

The script rotates the URL's RANDOMLY.

What I want is to have a rotator that goes to every url in the urlrotator.txt file the same number of times.

For example, if I put 2 urls is the urlrotator.txt file, I want the number of visits/hits to my site to be equally distributed.

I want the script to go to each url in the urlrotator.txt ONCE before it goes back to the first url in the script.

Thanks for the help.

Here's the script I have that randomly goes to a url in the text file:

<?php

$linksfile ="urlrotator.txt";

$urls = file("$linksfile");
$url = rand(0, sizeof($urls)-1);

$goto = "$urls[$url]";

header("Location: $goto");

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: URL Rotator Script Help

Post by Christopher »

Something like:

Code: Select all

<?php
session_start();
 
$linksfile ="urlrotator.txt";
$urls = file("$linksfile");
$maxurl = count($urls) - 1;
 
// reset counter if it is not set or greater than the number of urls
if ((! isset($_SESSION['urlrotator'])) || ($_SESSION['urlrotator'] > $maxurl)) {;
     $_SESSION['urlrotator'] = 0;
}
 
$goto = $urls[$_SESSION['urlrotator']];
 
// increment the counter
++$_SESSION['urlrotator'];
 
header("Location: $goto");
 
You could also use a Cookie.
(#10850)
arielpv1
Forum Newbie
Posts: 12
Joined: Mon Apr 21, 2008 7:57 pm

Re: URL Rotator Script Help

Post by arielpv1 »

It works!!!

Thank you :)

Best regards,

Ariel
arielpv1
Forum Newbie
Posts: 12
Joined: Mon Apr 21, 2008 7:57 pm

Re: URL Rotator Script Help

Post by arielpv1 »

I tested out the script and unfortunately, it always goes to the first url. It only works if you stay on the same computer and don't clear you cache & cookies. If I clear my cache and cookies, it always goes to the first url in the list. That means that eveyone who visits my site will see the same site always because it will most likely be the first time they visit it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: URL Rotator Script Help

Post by Christopher »

Then pick a random number between 0 and max for the starting value if the session variable is not defined.
(#10850)
arielpv1
Forum Newbie
Posts: 12
Joined: Mon Apr 21, 2008 7:57 pm

Re: URL Rotator Script Help

Post by arielpv1 »

Please forgive my ignorance, but how do I do that?

I'm very new to php, I'm sorry. I know it's something simple, but...

Thanks,

Ariel
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: URL Rotator Script Help

Post by John Cartwright »

Hint: Take a look at your original script, you were generating the random number there ;)
arielpv1
Forum Newbie
Posts: 12
Joined: Mon Apr 21, 2008 7:57 pm

Re: URL Rotator Script Help

Post by arielpv1 »

Thanks for all the help. I think I'll just use my original script which rotates to a random url in my urls file.

What I was trying to do was rotate through the list of url in my url file one at a time regardless of who visited it. Cleared cookies & cache or not. Just: on every execution of the file(visit to my site), the next url on the list displays until it gets to the last one, and the go to the first one in the list and so on.

Anyway, I don't even think if it's possible to do that. I think it should, but it's probably complicated(for some of us) :)

Have a nice day!

Ariel
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: URL Rotator Script Help

Post by John Cartwright »

Don't give up! Heres a little push in the right direction. :)

Code: Select all

 
$indexpath = '/path/to/index.txt';
 
$fp = fopen($indexpath, 'w+');
flock($fp, LOCK_EX)); //temporarily lock the file
$currentIndex = intval(fread($fp, filesize($indexpath))); //read contents as integer
 
//- load list of links
//- check to make sure the current index isn't greater than
//  the numer of links
//- redirect to the link corresponding to the current index 
 
fwrite($fp, $currentIndex+1); //increment the index by 1
flock($fp, LOCK_UN); //release the lock
fclose($fp);
 
examples have already been shown above regarding the comments in this snippet.
Post Reply