The title is quiet straightforward. I have to know on server side if the script called through HTTP request or by command line. I could examine the $_SERVER['argv']
or $_SERVER['argc']
.
What is the pragmatic way to do that?
相关问题
- Views base64 encoded blob in HTML with PHP
- Angular RxJS mergeMap types
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
But you have to send the data through http (tcp) anyway no matter if the script is called from cli or from a browser
Look at the keys in $_SERVER. If it is a cli request, you shouldn't see any that start with "HTTP".
Here is some simple test code:
And here is the output:
http://us3.php.net/manual/en/function.php-sapi-name.php
I suggest checking
if(isset($_SERVER['SERVER_NAME']))
Possibly checking if no
$_SERVER['HTTP_HOST']
is set? Because I believe that variable is populated through the Request Headers sent to a file on exection, and the command line probably doesn't send headers.You can check if the global variable
$argc
is set.