Friday, May 16, 2025

Restrict a PHP script so that it only runs via cron (CLI execution) OR cron based HTTP but prevent accessed from a browser (HTTP request)

 Restrict a PHP script so that it only runs via cron (CLI execution) OR cron based HTTP but prevent accessed from a browser (HTTP request)


define('APPLICATION_SERVER_IP', '123.123.123.123');

define('CRON_SECRET_KEY', 'e17Il1b9aA0Oa');


function writeToLog($message, $logLevel = 'INFO') {

    $logFile = __DIR__ . '/../logs/cron_logs.log'; // Change as needed

    if (!file_exists($logFile)) {

        touch($logFile);

        chmod($logFile, 0644);

    }

    $timestamp = date("Y-m-d H:i:s");

    $logMessage = "[$timestamp] [$logLevel] $message" . PHP_EOL;

    error_log($logMessage, 3, $logFile);

}


// Validate access

if (

    isset($_SERVER['REMOTE_ADDR'], $_GET['CRON_SECRET_KEY']) &&

    $_SERVER['REMOTE_ADDR'] === APPLICATION_SERVER_IP &&

    $_GET['CRON_SECRET_KEY'] === CRON_SECRET_KEY

) {

    echo "Safe execution";

    // Place your execution logic here

    writeToLog("Cron executed successfully.", "INFO");

} else {

    $ip = $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';

    writeToLog("Unauthorized hit from IP: $ip", "WARNING");

    http_response_code(403);

    exit("You don't have permission to access.");

}

Monday, September 5, 2022

Composer detected issues in your platform: Your Composer dependencies require a PHP version

 Composer detected issues in your platform: Your Composer dependencies require a PHP version 

Faster and quick solution 

Path: ~/.composer/vendor/composer/platform_check.php

if (!(PHP_VERSION_ID >= 70300)) {
    $issues[] = 'Your Composer dependencies require a PHP version ">= 7.3.0". You are running ' . PHP_VERSION . '.';
}

Here update/change php version id as your need

Monday, June 14, 2021

PHP Warning: session_start(): Failed to read session data: files (path: /var/cpanel/php/sessions/ea-phpXX)

 After upgrading the server/ PHP version/ Cpanel account transferring to a different server we might face this PHP Warning. It because of the session path-changing issue.

Example before transferring Cpanel account their was the variable session.save_path   might set different

Before session.save_path=/var/cpanel/php/sessions/ea-php56

After you might need to update based on your php version like 7.2 7.3

session.save_path=/var/cpanel/php/sessions/ea-php72

Or 

session.save_path=/var/cpanel/php/sessions/ea-php73

We can change this variable session.save_path path  using changing php ini file or using cpanel 

Login Cpanel  then

MultiPHP INI Editor >> Basic Mode" for each version of PHP installed on the system. However, ensure to revert this value back to "/var/cpanel/php/sessions/ea-php$$"

Here $$ means your current PHP version. example php71 /php72 / php73 / php74 



Total Pageviews