Click to See Complete Forum and Search --> : Randomizing a url weekly.....


MattchuB
12-12-2003, 10:32 PM
Okay, I'm trying to figure out a way to put a url into a div, but I want the url to randomize once a week. I want to have a set of urls that will switch once a week at random and I want to place it in a div without using javascript if at all possible, but if I have to, that's fine. Please help. Thanks. :)

pyro
12-13-2003, 09:22 AM
How 'bouts this?

<?PHP

# set up an array of the urls that you would like to use
$urls = array("http://www.ryanbrill.com/", "http://www.infinitypages.com/", "http://www.webdevfaqs.com");
$file = "current.txt"; # this will be automatically created

$time = 0;
$week = 60*60*24*7; # one week in seconds

if (file_exists($file)) {
$contents = file($file);
$contents = $contents[0];
list ($time, $url) = explode("|", $contents); # split the file
}

if ($time+$week < time()) { # if the time in the file + a week is less than the current time
$url = $urls[rand(0, count($urls)-1)]; # random url
$time = time();
$string = $time."|".$url;
# write new time and url to the file
$fp = fopen($file, "w");
flock($fp, LOCK_EX);
fwrite($fp, $string);
flock($fp, LOCK_UN);
fclose($fp);
}
echo "<div>$url</div>";

?>

MattchuB
12-13-2003, 04:15 PM
I tried that but it made nothing appear in the div. I changed the three urls to three of my own, but left the rest of it the same. All I did was copy and paste your code into the div in my page. Is there something more that I need to do? Or is it something in the code? Or do I need to change something other than just the urls? Please help. :confused:

pyro
12-13-2003, 04:47 PM
Just tried the code again -- works fine for me. Do you have a link?

MattchuB
12-13-2003, 04:50 PM
http://cs.clark.edu/~swingclub/newsitecss/csshtml.html

The blank section on the right is where the random urls should be. Thanks.

pyro
12-13-2003, 04:52 PM
PHP nearly always requires a .php extention... :)

MattchuB
12-13-2003, 04:54 PM
So, how do I make one of those?

pyro
12-13-2003, 04:58 PM
Just save the file with a .php extention. Most (I'd think ALL) HTML editors will be able to do this. If not, use notepad, and find a new editor.

MattchuB
12-13-2003, 05:00 PM
All I did was copy your code into the div, I didn't make a seperate file, if I do that, how do I link the div to it?

pyro
12-13-2003, 05:02 PM
Why make it a separate file? Just drop the code in, give the page a .php extention, and you should be good to go...

MattchuB
12-13-2003, 05:04 PM
so instead of an html page, I'll have a php page? Okay, if you say so....

pyro
12-13-2003, 05:06 PM
Yes, you are using PHP, so you have a .php extention... works nicely... ;)

MattchuB
12-13-2003, 05:10 PM
Okay, something bad happened. Go here: http://cs.clark.edu/~swingclub/newsitecss/csshtml.php and find out.

It appears to be a problem with this chunk of code: if (file_exists($file)) {
$contents = file($file);
$contents = $contents[0];
list ($time, $url) = explode("|", $contents); # split the file
}

pyro
12-13-2003, 05:25 PM
Try making a .txt file named current.txt and drop this into it:

0|http://www.someurl.com/

MattchuB
12-13-2003, 05:34 PM
Then what do I do? I just save a text doc with that url inside it?

pyro
12-13-2003, 05:46 PM
Yes, and upload it to the same directory as the script. The script should have worked by itself, though...

MattchuB
12-13-2003, 05:50 PM
I did that but it still doesn't work.

pyro
12-13-2003, 05:54 PM
Throw this into a .php file and give me a link:

<?PHP
phpinfo();
?>

MattchuB
12-14-2003, 02:43 AM
K, I made a php file called test.php and the link is: http://cs.clark.edu/~swingclub/newsitecss/test.php

pyro
12-14-2003, 10:00 AM
Hmm... everything looks good. Make sure the file current.txt is CHMODed to 666 (though the script should have worked fine, when there was no text file at all).

If none of that works, try specifying a root path to the file, for the $file variable.

MattchuB
12-14-2003, 02:28 PM
How do I CHMOD it to 666? And how do I specify a root path to the fine, for the $file variable?

pyro
12-14-2003, 03:39 PM
To CHMOD the file, you'll want to use an FTP program. You'll have to read how to do it in your specific program. Also, to set a root path, use something like this:

$file = $_SERVER['DOCUMENT_ROOT']."/absolute/path/to/file.txt";

So, if I had the file in my main directory, it would look like this:

$file = $_SERVER['DOCUMENT_ROOT']."/file.txt";

MattchuB
12-14-2003, 04:19 PM
I'm using WS_FTP and the only CHMOD thing that I can see is a section of boxes to check, there is no place to enter a value. Also, do I put the root path thingy in the current.txt file or the .php file with the random thing in it?

pyro
12-14-2003, 04:27 PM
Set it to read and write for Owner, Group, and Other, or rw-rw-rw.

Also, the root path will go in the .php file.

MattchuB
12-14-2003, 04:31 PM
When I set it to rw rw rw, it gave me an error that said "553 Permission denied on server. (chmod) ! Chmod failed. It mat not be supported on remote site." What does that mean? Also, where in the .Php file should I put the root path?

MattchuB
12-14-2003, 04:36 PM
Never mind that last part, I found where to put the absoulte root path. It still doesn't work though. Heres the url: http://cs.clark.edu/~swingclub/newsitecss/csshtml.php

pyro
12-14-2003, 04:37 PM
Originally posted by MattchuB
When I set it to rw rw rwHow did you set it?

For the script, you can set the root path on line 5...

MattchuB
12-14-2003, 04:39 PM
I right clicked the file in the ftp window, then clicked chmod (unix) then checked the boxes for read and write on owner group and other, just like you said.

pyro
12-14-2003, 04:41 PM
Please try posting your modified script.

MattchuB
12-14-2003, 04:42 PM
K, this is just what's inside the div on the main page: <?PHP

# set up an array of the urls that you would like to use
$urls = array("http://cs.clark.edu/~swingclub/newsitecss/rlyndsey.html", "http://cs.clark.edu/~swingclub/newsitecss/ralison.html", "http://cs.clark.edu/~swingclub/newsitecss/rmatty.html");
$file = $_server['document_root']."/newsitecss/current.txt"; # this will be automatically created

$time = 0;
$week = 60*60*24*7; # one week in seconds

if (file_exists($file)) {
$contents = file($file);
$contents = $contents[0];
list ($time, $url) = explode("|", $contents); # split the file
}

if ($time+$week < time()) { # if the time in the file + a week is less than the current time
$url = $urls[rand(0, count($urls)-1)]; # random url
$time = time();
$string = $time."|".$url;
# write new time and url to the file
$fp = fopen($file, "w");
flock($fp, LOCK_EX);
fwrite($fp, $string);
flock($fp, LOCK_UN);
fclose($fp);
}
echo "<div>$url</div>";

?>

pyro
12-14-2003, 04:56 PM
Change this:

$file = $_server['document_root']."/newsitecss/current.txt"; # this will be automatically created
to this:

$file = $_SERVER['DOCUMENT_ROOT']."/newsitecss/current.txt"; # this will be automatically created

MattchuB
12-14-2003, 05:01 PM
Okay, I changed that, but it still has an error, it's a bit different now, the first error is longer, but that's all.

MattchuB
12-14-2003, 05:06 PM
If you want, I can attach a zip file with all the code for the site in it and you can look at it....