Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

php - Passing $_GET parameters to cron job

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...