我是 cron 工作的新手,不确定这是否可行。
出于安全目的,我考虑制作一个单页脚本来查找某些 GET 值(用户名、密码和安全代码),以确保只有计算机和知道所有这 3 个值的人才能运行该命令。
我制作了脚本,它可以在浏览器中运行它,但是否可以使用 GET 值运行 cron 作业?
我运行就是一个例子
* 3 * * * /path_to_script/cronjob.php?username=test&password=test&code=1234
这可能吗?
最佳答案
$_GET[]
和 $_POST[]
关联数组仅在通过 Web 服务器调用您的脚本时才被初始化。通过命令行调用时,参数在 $argv
中传递。数组,就像 C 一样。
Contains an array of all the arguments passed to the script when running from the command line.
你的命令是:
* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234
然后您将使用 parse_str()设置和访问参数:
<?php
var_dump($argv);
/*
array(4) {
[0]=>
string(27) "/path_to_script/cronjob.php"
[1]=>
string(13) "username=test"
[2]=>
string(13) "password=test"
[3]=>
string(9) "code=1234"
}
*/
parse_str($argv[3], $params);
echo $params['code']; // 1234
关于php - 将 $_GET 参数传递给 cron 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297945/