将$ _GET参数传递给cron作业 [英] Passing $_GET parameters to cron job
问题描述
我是cron新工作,我不知道这是否会工作。
出于安全考虑,我考虑制作一个单页脚本寻找某些GET值(用户名,密码和安全代码),以确保只有计算机和知道所有3的人都可以运行命令。
我做了脚本它可以在浏览器中运行它,但是可以用GET值运行cron作业吗?
一个例子是我运行
* 3 * * * /path_to_script/cronjob.php?username=test&password=test&code=1234
这是否有可能?
解决方案c $ c> $ _ GET [] &
$ _ POST []
关联数组仅在通过Web服务器调用脚本时初始化。通过命令行调用时,参数会传递到$ argv
数组,就像C一样。
包含传递给脚本的所有参数的数组
您的命令将是:
* 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
I am new at cron jobs and am not sure whether this would would work.
For security purposes I thought about making a one page script that looks for certain GET values (a username, a password, and a security code) to make sure that only the computer and someone that knows all 3 can run the command.
I made the script and it works running it in a browser but is it possible to run the cron job with GET values?
a example would be me running
* 3 * * * /path_to_script/cronjob.php?username=test&password=test&code=1234
Is this possible?
解决方案The
$_GET[]
&$_POST[]
associative arrays are only initialized when your script is invoked via a web server. When invoked via the command line, parameters are passed in the$argv
array, just like C.Contains an array of all the arguments passed to the script when running from the command line.
Your command would be:
* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234
You would then use parse_str() to set and access the paramaters:
<?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
这篇关于将$ _GET参数传递给cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!