URL Rotator Script Help
Moderator: General Moderators
URL Rotator Script Help
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");
?>
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");
?>
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: URL Rotator Script Help
Something like:
You could also use a Cookie.
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");
(#10850)
Re: URL Rotator Script Help
It works!!!
Thank you
Best regards,
Ariel
Thank you
Best regards,
Ariel
Re: URL Rotator Script Help
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: URL Rotator Script Help
Then pick a random number between 0 and max for the starting value if the session variable is not defined.
(#10850)
Re: URL Rotator Script Help
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
I'm very new to php, I'm sorry. I know it's something simple, but...
Thanks,
Ariel
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: URL Rotator Script Help
Hint: Take a look at your original script, you were generating the random number there 
Re: URL Rotator Script Help
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: URL Rotator Script Help
Don't give up! Heres a little push in the right direction. 
examples have already been shown above regarding the comments in this snippet.
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);