PHP code to stop the script to run from the browser

Envoirnment: Linux, PHP

Scenario: I wrote a script to be installed as cron. Now the thing is, I want the script to be run only through cron, and not through any browser (any web browser, including mobile browsers). Therefore, I am looking for a function similar to browserValidate.

The script is written in MVC and will run as

/usr/bin/GET http://xyz.com/abc/pqr

Please help me with this.

Thanks in advance.

+5
source share
5 answers

@Mob, script , -. - , , script - .

( ):

$isRunningFromBrowser = !isset($GLOBALS['argv']);

, $isRunningFromBrowser , / /.

+8

script crontab, supgllobal $_SERVER Apache (HTTP_*), - $_SERVER - :

# in the crontab
FOOVAR=1 /usr/bin/php5 script.php

, script.php, FOOVAR:

if (!isset($_SERVER['FOOVAR']))
   die('No browser access.');

cronjob wget, IP - , .htaccess:

SetEnvIf Remote_Addr "192.168.0.1" FOOVAR=1

FOOVAR, IP- "192.168.0.1".

+6

, :

/usr/bin/GET HTTP, , : -, , PHP , cron-. -, , /usr/bin/GET cron .

URL- script :

/usr/bin/GET http://xyz.com/abc/pqr/SomeUniqueIdentifier

-H /usr/bin/GET, User-Agent , :

/usr/bin/GET -H "User-Agent: SomeUniqueIdentifier" http://xyz.com/abc/pqr

PHP , :

if (strcmp($_SERVER['HTTP_USER_AGENT'], "SomeUniqueIdentifier") == 0)
{
   // do whatever
}
else
{
   // do nothing
   exit();
}

100%, .

+1

- IP- , ip.

, - ( , ..), , script.

+1

All Articles