10

... 或者反过来,有没有办法知道 php 脚本是否在 Web 服务器中运行?

4

4 回答 4

36

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

function is_cli()
{
    return php_sapi_name() === 'cli';
}
于 2009-03-03T17:43:55.087 回答
7

通常,在 CLI 模式下运行时,将设置超全局变量$argv$argc,并且其中的许多典型内容$_SERVER(例如请求方法)将不可用。此外,还将设置预定义的控制台流,STDIN例如和STDOUTSTDERR

于 2009-03-03T17:41:50.270 回答
3

您可以检查 $_SERVER 变量...例如:

$_SERVER['SERVER_ADDR']

如果它不是通过网络服务器执行的,这将是空的。

于 2009-03-03T17:42:21.707 回答
1

我写了一个只包含这一行的小 php 脚本:

print_r($_SERVER);

当我在命令行中执行它时,这是我输出的一部分:

rascher@eggs:~$ php test.php 
Array
(
    [TERM] => xterm
    [SHELL] => /bin/bash
    [SSH_CLIENT] => 192.168.1.104 57547 22
    [SSH_TTY] => /dev/pts/1
    [USER] => rascher
    [LS_COLORS] => no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:
    [MAIL] => /var/mail/rascher
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    [PWD] => /home/rascher
    [LANG] => en_US.UTF-8
    [HISTCONTROL] => ignoreboth
    [SHLVL] => 1
    [HOME] => /home/rascher
    [LOGNAME] => rascher
    [SSH_CONNECTION] => 192.168.1.104 57547 192.168.1.105 22
    [LESSOPEN] => | /usr/bin/lesspipe %s
    [LESSCLOSE] => /usr/bin/lesspipe %s %s
    [_] => /usr/bin/php
    [PHP_SELF] => test.php
    [SCRIPT_NAME] => test.php
    [SCRIPT_FILENAME] => test.php
    [PATH_TRANSLATED] => test.php
    [DOCUMENT_ROOT] => 
    [REQUEST_TIME] => 1236100063
    [argv] => Array
        (
            [0] => test.php
        )

    [argc] => 1
)

在网络服务器上运行相同的东西给了我这个:

Array
(
    [HTTP_HOST] => xxxx.com
    [HTTP_USER_AGENT] => Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009020911 Ubuntu/8.10 (intrepid) Firefox/3.0.6
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5
    [HTTP_ACCEPT_ENCODING] => gzip,deflate
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [HTTP_KEEP_ALIVE] => 300
    [HTTP_CONNECTION] => keep-alive
    [HTTP_PRAGMA] => no-cache
    [HTTP_CACHE_CONTROL] => no-cache
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin
    [SERVER_SIGNATURE] => <address>Apache/2.2.6 (Fedora) Server at xxxx.com Port 80</address>

    [SERVER_SOFTWARE] => Apache/2.2.6 (Fedora)
    [SERVER_NAME] => xxxx.com
    [SERVER_ADDR] => 208.109.29.70
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 69.134.191.151
    [DOCUMENT_ROOT] => /xxxx/httpdocs
    [SERVER_ADMIN] => jpg@gfhome.com
    [SCRIPT_FILENAME] => /xxxx/args.php
    [REMOTE_PORT] => 52187
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /args.php
    [SCRIPT_NAME] => /args.php
    [PHP_SELF] => /args.php
    [REQUEST_TIME] => 1236102678
)

值得注意的是这些变量,它们最终可能是最有用的:

[argv] => Array
    (
        [0] => test.php
    )

[argc] => 1
于 2009-03-03T17:55:34.810 回答