Page 1 of 1

URL Rotator Script Help

Posted: Mon Apr 21, 2008 8:07 pm
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");

?>

Re: URL Rotator Script Help

Posted: Mon Apr 21, 2008 9:18 pm
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.

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 12:11 am
by arielpv1
It works!!!

Thank you :)

Best regards,

Ariel

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 7:28 pm
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.

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 7:55 pm
by Christopher
Then pick a random number between 0 and max for the starting value if the session variable is not defined.

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 8:14 pm
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

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 9:02 pm
by John Cartwright
Hint: Take a look at your original script, you were generating the random number there ;)

Re: URL Rotator Script Help

Posted: Tue Apr 22, 2008 10:39 pm
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

Re: URL Rotator Script Help

Posted: Wed Apr 23, 2008 11:18 am
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.