tags:

views:

250

answers:

6

Hi,

I have a common script which Im including in my php cron files and the files which are accessing through the browser. Some part of the code, I need only for non cron files. How can I detect whether the execution is from CLI or through browser (I know it can be done by passing some arguments with the cron files but I dont have access to crontab). Is there any other way ?

Regards Binoy

A: 

I think you can see it from the $_SERVER variables. Try to print out the $_SERVER array for both browser & CLI and you should see differences.

Jimmy Shelter
+2  A: 

consult PHP_SAPI

just somebody
A: 

You can use:

if (isset($argc))
{
    // CLI
}
else
{
    // NOT CLI
}
e-t172
-1 - `$argc` could have been set within the application, couldn't it? Not a reliable method. Cost me half a day's work once. `php_sapi_name()` is the only good way I know of.
Pekka
+3  A: 

Perhaps you should peruse the search feature next time before asking: How to determine if a PHP file is loadad via cron/command-line

Jani Hartikainen
+5  A: 

http://ru.php.net/manual/en/function.php-sapi-name.php

Comma
+1 the only right way to do it.
Pekka
+2  A: 
if(php_sapi_name() == "cli") {
    //In cli-mode
} else {
    //Not in cli-mode
}
Linus Unnebäck