tags:

views:

289

answers:

4

... or the other way around, is there any way to know if a php script is running inside a web server?

+4  A: 

Typically, when running in CLI mode, the superglobals $argv and $argc will be set, and many of the typical contents of $_SERVER (e.g. request method) won't be available. In addition, pre-defined console streams such as STDIN, STDOUT and STDERR will be set up.

Rob
+1  A: 

You could check the $_SERVER variables ... such as:

$_SERVER['SERVER_ADDR']

This will be empty if it is not executed through a web server.

jonstjohn
+9  A: 

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

function is_cli()
{
    return php_sapi_name() === 'cli';
}
Ionuț G. Stan
A: 

I wrote a small php script that only contained this line:

print_r($_SERVER);

When I executed it in the commandline, this was part of my output:

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
)

Running the same thing on the webserver gave me this:

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] => [email protected]
    [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
)

Of note are these vars, which might end up being the most useful:

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

[argc] => 1
rascher