Created
August 24, 2019 19:51
-
-
Save craigedmonds/94c019928d514141c06f88bf1e8de40e to your computer and use it in GitHub Desktop.
Allow a script to run only between two times and email on error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
######################## | |
// allow a script to only be run between two times | |
// EG: you can use this for a cron job and if someone tries to access it outside the times | |
// then you can block the request. | |
######################## | |
header('Content-type: application/json; charset=utf-8'); //set the page to text only (optional) | |
$start_time = "09:00"; | |
$end_time = "09:30"; | |
$server_time_zone = date_default_timezone_get(); //this is the current server timezone | |
date_default_timezone_set('Europe/Madrid'); //change the server timezone to a custom time | |
$script_time_zone = date_default_timezone_get(); //confirm the new timezone | |
$now = new DateTime(); //get the time of the server right now | |
$begin = new DateTime($start_time); //set the first time | |
$end = new DateTime($end_time); / set the second tim | |
if ($now >= $begin && $now <= $end) { | |
$cron_can_run = true; | |
} else { | |
$cron_can_run = false; | |
} | |
//if cron cant run, hard exit | |
if($cron_can_run == false) { | |
$time_now = date('H:i a'); | |
echo "Error - cron cannot run.\n"; | |
echo "Cron can only run between the hours of $start_time and $end_time and current time is: $time_now.\n"; | |
echo "Times are based on $script_time_zone time zone.\n"; | |
echo "If you continue to see this error please contact [email protected]."; | |
//if the request failed and its from cpanl cron you can send an email | |
if($_GET["request"] == "cpanel-cron") { | |
$to = "[email protected]"; | |
$from = "[email protected]"; | |
$subject = "error with cron on content system at $time_now"; | |
$message .= "<strong style='color:red;'>ERROR WITH CRON ON CONTENT SYSTEM</strong>"; | |
$message .= "<br><br>"; | |
$message .= "Cpanel tried to make a request to the cron located at: xxxxxxxx"; | |
$message .= "<br><br>"; | |
$message .= "The request failed so customers were not sent their emails."; | |
$message .= "<br><br>"; | |
$message .= "Here is some additional data for you to troubleshoot."; | |
$message .= "<br><br>"; | |
$message .= "Server time zone: $server_time_zone<br>"; | |
$message .= "Script time zone: $script_time_zone<br>"; | |
$message .= "Time of request: $time_now<br>"; | |
$message .= "Script is allowed to run between $start_time and $end_time<br>"; | |
$message .= "IP of request: ". $_SERVER['REMOTE_ADDR'] . "<br>"; | |
$message .= "<br><br>"; | |
$message .= "<--- end of notifcation"; | |
$message .= "</body></html>"; | |
//build the html headers | |
$headers = "From: " . $from . "\r\n"; | |
$headers .= "Reply-To: ". $reply_to . "\r\n"; | |
$headers .= "BCC: ". $bcc. "\r\n"; | |
$headers .= "MIME-Version: 1.0\r\n"; | |
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; | |
mail($to, $from, $reply_to, $bcc, $subject, $message, $headers); | |
echo "\n\nNOTICE: Alert email sent to [email protected]."; | |
} | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment