Jump to content

Running a script once a day


Froskoy

Recommended Posts

Hi,

 

I have a page that gets about 1000 hits a day.

 

The first time the page is viewed on a particular day, I should like to run a script which doesn't do anything else the other 999 times it is viewed.

 

Is this possible and how do I go about doing it?

 

Thanks very much,

 

Froskoy.

Link to comment
https://forums.phpfreaks.com/topic/175953-running-a-script-once-a-day/
Share on other sites

Could do something like so at the top of the script:

 

<?php

// Connect to Database
include("includes/connect.php");

// Select the row according to date
$date = date("d-m-Y");
$query = mysql_query("SELECT * FROM firsthit WHERE date='".$date)."'");
if(mysql_num_rows($query) == 0) {
    // Run script and add new row
    include("script.php");
    $update = mysql_query("INSERT INTO firsthit ('id', 'date') VALUES (NULL, '".$date."')");
}

?>

 

Table structure:

 

id (smallint - autoincrement) | date (could be varchar or date)

Does it have to be triggered by the first request?  If not, you could simply schedule a cron for run first thing each day.

 

If that's not a possibility then you can use mattal's suggestion (as I don't know any that will work without querying the DB every time), but I would use a DATETIME type, not a VARCHAR.

If not using a database, you could use the same logic that mattal999 posted above but use a flat file to keep track of whether the script had been run that day (e.g. change the content of the file to the current date). Although, I would change the order of operations to update the trigger after the processes you want to run once have completed.

 

Alternatively, you could create two files - one that is accessed by the users wich just displays the information and another which performs the scripting you want run once per day. Then use a cron job to schedule that script to run each day at a specified time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.