-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathphp_is_cli.php
More file actions
27 lines (24 loc) · 829 Bytes
/
php_is_cli.php
File metadata and controls
27 lines (24 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
/*
Purpose : Check if PHP script is called from CLI, obtain local server IP
Author : Ap.Muthu <apmuthu@usa.net>
Release : 2018-04-27
Reference: https://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server
*/
function is_cli() {
if ( defined('STDIN') ) return true;
if ( php_sapi_name() === 'cli' ) return true;
if ( array_key_exists('SHELL', $_ENV) ) return true;
if ( empty($_SERVER['REMOTE_ADDR']) &&
!isset($_SERVER['HTTP_USER_AGENT']) &&
count($_SERVER['argv']) > 0
) return true;
if ( !array_key_exists('REQUEST_METHOD', $_SERVER) ) return true;
return false;
}
function local_ip() {
if (is_cli())
return getHostByName(getHostName());
else
return getHostByName(getHostName($_SERVER['SERVER_ADDR']));
}