質問

How can I check in my PHP script, that script execute exactly from Cron Tab by wget util, not from web browser by any user?

役に立ちましたか?

解決

There is no reliable solution. Anything wget can do, your browser can do too.

Your best shot is something like sending to wget to http://example.com/mysript.php?accesskey=some+really+obscure+password+that+only+you+should+know and check for that access key in your script. Of course, if anyone gets the password, this kind of protection is useless, but it's a far more consistent point of failure than blindly relying on User-Agent sniffing.

他のヒント

A possibility would be to use $argv. Check if $argv[1] is a certain value and call the script from crontab with the value as argument, like so: script.php argument1.

You're question is a bit difficult to understand bus I guess you wan't to make sure a PHP script is requested by wget (that get initiated by cron)

Although it might be more efficient to call the PHP script directly by cron in this case you could check the server's logging end search for the user agent matching something like wget.

An insecure solution would be to check the headers for the User-Agent:

wget -d http://www.google.com/

---request begin---
GET / HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: www.google.com
Connection: Keep-Alive

---request end---

So you could do:

<?php

$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (strstr($userAgent, 'Wget')) {

   // wget Request

}

You can pass some arguments in crontab for your script http://php.net/manual/reserved.variables.argv.php

Then checking for these args you'll know if your script is used from command line or web.

EDIT :

Seeing answers, let's make this clear.

Calls with Wget or cURL or whatever HTTP GET request WON'T PASS ARGS! ARGS will only pass with local call (like : php script.php arg1 arg2).

Please, noobs, stop talking when you don't know about it, and try it out yourself on your server if you aren't sure about it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top