Home » Php » php – Running the page that needs time for the result with cronjob

php – Running the page that needs time for the result with cronjob

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a php page (the code below) and with this page I get information from 70 sites and send a mail. This information is about the version of the site and the size of its files.

When I call my control page manually in the browser, the process is completed in about a minute and an email is sent to all sites.

However, when I call with a cronjob, the mail comes up, but probably because it doesn’t get the information, it doesn’t.

Probably the cronjob does not work like manual calling.
What can I do?

    $yp_PathRoot = $_SERVER['DOCUMENT_ROOT'];
session_start();

$yp_PathSettings = '/panel/functions/settings.php';
include_once($yp_PathRoot . $yp_PathSettings);

function zCheck($myWebSite){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://$myWebSite/panel/?qc=1");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    $output2 = curl_exec($ch);
    curl_close($ch); 
    return $output2;
}

$array = array(
    "domain1.ext",
    "domain2.ext",
    ...
    "domain68.ext",
    "domain69.ext",
    "domain70.ext"
);

$myMessage = "";
foreach($array as $value)
{
    $checkedSite    = $value;
    $checkedValues  = zCheck($value);
    $exlodedValues  = explode('|', $checkedValues);

    $myCase                     = $exlodedValues[0];
    $klySoftwareSizeSF          = $exlodedValues[1];
    $errorAcceptableSize        = $exlodedValues[2];
    $showQuotaYakinSF           = $exlodedValues[3];
    $klyThemeAndImagesSizeSF    = $exlodedValues[4];
    $showQuotaHesapliSF         = $exlodedValues[5];
    $fromMailer                 = $exlodedValues[6];
    $myUserName                 = $exlodedValues[7];

    switch($myCase) {
        case 1:
            $statusMessage  = "There is an increase in the size of non-theme files. Check the error files. If there is no error file, look for another file with a larger size.";
            $quotaClass     = " style=\"color:black;\"";
            $softwareClass  = " style=\"color:red;\"";
            break;
        case 2:
            $statusMessage = "User uploaded images and files are about to fill the quota.";
            $quotaClass     = " style=\"color:orange;\"";
            $softwareClass  = " style=\"color:black;\"";
            break;
        case 3:
            $statusMessage = "The size of the images and files uploaded by the user has filled the quota.";
            $quotaClass     = " style=\"color:red;\"";
            $softwareClass  = " style=\"color:black;\"";
            break;
        case 4:
            $statusMessage = "Everything is OK.";
            $quotaClass     = " style=\"color:black;\"";
            $softwareClass  = " style=\"color:black;\"";
            break;
    }
    $myMessage =  $myMessage."<br/><b>".$checkedSite."</b><br/>Site checked. ".$statusMessage."<br/><table><tr $softwareClass><td>Software Size<td><td>: $klySoftwareSizeSF<td></tr><tr $quotaClass><td>Size of Themes and Images<td><td>: $klyThemeAndImagesSizeSF<td></tr><tr $quotaClass><td>Site Quota<td><td>: $showQuotaHesapliSF<td></tr></table>";
}

$fromMailer         = "\"Postman\" <[email protected]>";
$myUserName         = "Postman";
$mailFuncFrom       = "$fromMailer";
$mailFuncTo         = "\"Postman\" <[email protected]>";
$mailFuncSubject    = "Daily Quota Check";
$clientServerName   = $_SERVER['SERVER_NAME'];
$clientRequestUrl   = $_SERVER['REQUEST_URI'];
$clientUserName     = $myUserName;
$mailFuncContent    = "<b>Site:</b> $clientServerName<br/><b>> The page on which the message was sent:</b> $clientRequestUrl<br/><b>User:</b> $clientUserName<br/><br/>Hello,<br/>Daily check completed. The status of the sites is as follows.<br/><br/>$myMessage";
$mailSendMethod     = 1;

sendMyMail($mailFuncFrom,$mailFuncTo,$mailFuncSubject,$mailFuncContent,$mailSendMethod);
How to&Answers:

This is most likely because you are using php features intended for the web in a cron script, which is run with php-cli.

Many web-specific features such as sessions ($_SESSION and its related functions), cookies, some web-specific $_SERVER variables such as $_SERVER['DOCUMENT_ROOT'] and $_SERVER['HTTP_HOST'] and more are not available in cron jobs.

You should either setup a new cron to make a web request to your current cron (if that makes sense), or modify the cron to not use web-specific php features.