Google Answers Logo
View Question
 
Q: simple auto updating script for a web page ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: simple auto updating script for a web page
Category: Computers > Programming
Asked by: mistygirl-ga
List Price: $10.00
Posted: 26 Nov 2005 04:03 PST
Expires: 26 Dec 2005 04:03 PST
Question ID: 597708
I want a simple script that will automatically update a web page for
me from a list of html pages I've made in advance. No embedded code in
the pages, etc.

I just want the script to take one html page from a list like
"day1.html day2.html day3.html" in one folder on my website and move /
replace it over index.html every day at midnight.

I want a normal .html page (no .shtml or .php etc) with different
content daily and not have to update it myself every day (or have my
ftp program running waiting to update).

My guess is a PHP script using cron would be the easiest, but I really
don't care what language it's in, or how it's done.

Where do I buy a script that will do this? Or for something as simple
as this, how/where do I hire someone to write it for me?
Answer  
Subject: Re: simple auto updating script for a web page
Answered By: palitoy-ga on 26 Nov 2005 05:26 PST
Rated:5 out of 5 stars
 
Hello mistygirl-ga

Thank-you for your question.

I have written a small script for you that should do as you require. 
It was not clear how the script should take the html page from the
list so I have taken my best guess which was to pick a random file
from the directory.  If this is not how you wish to achieve this
please ask for clarification and state how you would like it to be
done.

The following script should be saved in a directory or folder
containing the html files and be called "random.php".  A cron job
should then be set up to run the script every day.  The script will
randomly select a file from the directory and save it as index.html in
the directory above it.

You may need to tweak the file and file permissions on your server in
order for it to work precisely as you require but should you need any
further assistance please do not hesitate to ask for clarification.

<?php
// read all the files in the directory into an array
$handle=opendir("."); 
$dirarray = array();
while ($file = readdir($handle)) 
{
// make sure the file is not this one!
if ($file > ".." && $file != "test.php" ) {array_push($dirarray,$file);}
}
closedir($handle);

// pick a random file and save it as index.html
// in the directory above this one
srand ((double) microtime() * 10000000);
if (!copy($dirarray[array_rand($dirarray)], "../index.html")) {
   echo "failed to copy...\n";
}
?>

Request for Answer Clarification by mistygirl-ga on 26 Nov 2005 17:18 PST
wonderful!
Only problem is I don't want it to be random..... how do I tweak it to
take the html files in order? (take day1.html, first day, day2.html
second.... etc.)

Clarification of Answer by palitoy-ga on 27 Nov 2005 01:33 PST
Can you tell me a little bit more about the list of files?  Do you
have a certain page on a certain day of the month?  How many files are
there?  Does the list change?

Clarification of Answer by palitoy-ga on 27 Nov 2005 02:02 PST
I was thinking of implementing this like this:

<?php
// create an array with the files you wish to use in
$dirarray[1] = "day1.html";
$dirarray[2] = "firstday.html";
$dirarray[3] = "day2.html";
..etc..
$dirarray[31] = "day31.html";

// copy the element in the array corresponding to today's date
if (!copy($dirarray[date("j")], "../index.html")) {
   echo "failed to copy...\n";
}
?>

To use this you would need to set up the array to have 31 elements
(one for each day of the month.  The script would then take the date
and copy the page you wished depending on the day of the month.

Let me know if this is of use or how you would like it to be
implemented in more detail (as per my previous clarification).

Request for Answer Clarification by mistygirl-ga on 28 Nov 2005 20:16 PST
31 days will be fine.... :-)
Just to be clear here:

So the final script is called test.php, and it's placed in a folder
called random.php with 31 html pages named day1.html day2.html etc.

Then I have to get my system admin to set up cron to run test.php once
a day, and walk away.

Is that right?

One more quick question... if it fails to copy.... what happens?

Clarification of Answer by palitoy-ga on 29 Nov 2005 02:17 PST
That is nearly correct!  Simply save the script as random.php into a
folder containing all the HTML files.  (You will need to add the
additional names of the files to the beginning of the script.)  Then
simply instruct your system admin to have the random.php script run by
cron as you suggested.

If the script fails to copy a message will be printed saying "failed
to copy...", your system admin should be able to set it up so that you
receive an email each time the script is executed.  Obviously if the
script fails to copy the index.html page will not be changed.

Let me know if you need anything else.
mistygirl-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
Thank you. Awesome. :-)

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy