简体   繁体   English

只有cron才能运行php脚本

[英]run php script only by cron

I know how to run a script with a cron, but what I need is to be able to run my script only by a cron. 我知道如何使用cron运行脚本,但我需要的是只能通过cron运行我的脚本。

Thank you! 谢谢!

As explained in this duplicate thread: 如此重复帖子中所述:

PHP & cron: security issues PHP和cron:安全问题

You should keep this file outside of public_html. 您应该将此文件保留在public_html之外。

Sometimes, though, this is not possible. 但有时候,这是不可能的。 My mind went to Moodle , where a similar feature exists. 我的脑子去了Moodle ,那里有类似的功能。 This is what they do. 这就是他们所做的。

From cron.php : 来自cron.php

...

/// The current directory in PHP version 4.3.0 and above isn't necessarily the
/// directory of the script when run from the command line. The require_once()
/// would fail, so we'll have to chdir()

    if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
        chdir(dirname($_SERVER['argv'][0]));
    }

...

/// check if execution allowed
    if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web.
        if (!empty($CFG->cronclionly)) { 
            // This script can only be run via the cli.
            print_error('cronerrorclionly', 'admin');
            exit;
        }
        // This script is being called via the web, so check the password if there is one.
        if (!empty($CFG->cronremotepassword)) {
            $pass = optional_param('password', '', PARAM_RAW);
            if($pass != $CFG->cronremotepassword) {
                // wrong password.
                print_error('cronerrorpassword', 'admin'); 
                exit;
            }
        }
    }

...

You should keep this script outside of the public folder. 您应该将此脚本保留在公用文件夹之外。 Also, set appropriate permissions to the file so public users can not execute the script. 此外,为文件设置适当的权限,以便公共用户无法执行脚本。 Put below code snippet to the top of your script. 将下面的代码段放在脚本的顶部。

if(php_sapi_name() !== 'cli'){
    die('Can only be executed via CLI');
}

Note that you need to use the full path to the PHP executable when you setup your cron job. 请注意,在设置cron作业时,需要使用PHP可执行文件的完整路径。 Ex : /usr/local/bin/php (Your path may be differ from this) 例如:/ usr / local / bin / php(您的路径可能与此不同)

You need a PHP CLI/CGI executable for that. 您需要一个PHP CLI / CGI可执行文件。 Assuming that the php program is located at /usr/local/bin/php , you can use: 假设php程序位于/usr/local/bin/php ,您可以使用:

/usr/local/bin/php /path/to/your/script.php

See also: http://nl.php.net/manual/en/features.commandline.usage.php 另见: http//nl.php.net/manual/en/features.commandline.usage.php

Please add this script at the top of your PHP file: 请在PHP文件的顶部添加此脚本:

$isCLI = ( php_sapi_name() == 'cli' );
if( !$isCLI )
    die("Sorry! Cannot run in a browser! This script is set to run via cron job");

and then if you try to run the PHP file via the browser, you cannot run it. 然后,如果您尝试通过浏览器运行PHP文件,则无法运行它。 This error message will be displayed. 将显示此错误消息。 But at the same time, it can be run via a cron job. 但与此同时,它可以通过cron作业运行。

Try to grant execute permissions only for the cron daemon user, maybe with that you get what you want. 尝试仅为cron守护程序用户授予执行权限,也许可以获得所需的权限。

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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