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
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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.