简体   繁体   中英

PHP Execute Code once every 24 hours

I am trying to create a PHP code that will execute once every 24 hours. The current CMS that I am using supports this, and it works correctly, whereas, I want to make it a little more complicated.

For one hour, every day, I want a code to execute, which will give special permissions to my users during this one hour.

Once the hour is up, I would like it to revert the changes, taking away the permissions. The users would then need to wait until the next day, when the hour is back.

For example:

<?php

if (!defined('UBER') || !UBER)
{
    exit;
}

dbquery("UPDATE catalog_pages SET visible = '1' WHERE id='91'");
$core->Mus('update_catalogue');

?>

This code executes once every 24 hours, and works fine. It updates the catalogue setting a page to be visible to everyone. I am only stuck on how to make it disappear after the hour is up.

Lets say that the hour is 4.00am to 5.00am.

How would I make the page disappear after 5.00am using a PHP code?

Thanks in advance.

if (date("G") == '4'){
//give
}elseif (date("G") == '5'){
//takeway
}

or in the query

.. WHERE HOUR(NOW()) BETWEEN 4 AND 5

This is actually really easy to do. When the user logs on or tries to use a permission that is only granted at the certain time, run a check to see if the time is within those hours. If your CMS allows you to intercept events, use something like this:

onUserAction(){
    if(isSpecialHour()){
        denyPermission()...
    }
    else{
        grantPermission()...
    }
}

When the user tries to do something that requires a permission, the system should check if it is within that special hour period and then either grant or deny permission.

With PHP alone, it is impossible to have a script run at a specific interval. PHP scripts are run when they are loaded, and unless you have someone run the scrip at that exact time (or use something like cron) it can not be done. Of course, you could use cron or simmilar to accomplish the same thing. The above method is probably your best bet though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM