php_sapi_name

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

php_sapi_name웹 서버와 PHP 사이의 인터페이스 형식을 반환

설명

string php_sapi_name ( void )

PHP가 사용하는 인터페이스(서버 API, SAPI) 형식을 나타내는 소문자 문자열을 반환합니다. 예를 들어, CLI PHP에서 이 문자열은 "cli"이고, 아파치에서는 사용하는 SAPI에 따라 여러 값일 수 있습니다. 가능한 값은 아래 목록에 있습니다.

반환값

인터페이스 형식을 소문자 문자열로 반환합니다.

완전하진 않지만, 가능한 반환값은 aolserver, apache, apache2filter, apache2handler, caudium, cgi(PHP 5.3까지), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, webjames를 포함합니다.

예제

Example #1 php_sapi_name() 예제

이 예제는 서브문자열 cgi를 확인합니다. cgi-fcgi일 수도 있기 때문입니다.

<?php
$sapi_type 
php_sapi_name();
if (
substr($sapi_type03) == 'cgi') {
    echo 
"CGI PHP를 사용하고 있습니다.\n";
} else {
    echo 
"CGI PHP를 사용하고 있지 않습니다.\n";
}
?>

주의

Note: 대체 접근법

PHP 상수 PHP_SAPIphp_sapi_name()과 같은 값을 가집니다.

Tip

이런 일이!

정의된 SAPI는 명백하지 않을 수 있습니다. 예를 들면, apache 대신 apache2handlerapache2filter로 정의될 수 있기 때문입니다.

참고

add a note add a note

User Contributed Notes 4 notes

up
34
michal at roszka dot pl
15 years ago
The php_sapi_name() function is extremely useful when you want to determine the type of interface. There is, however, one more gotcha you need to be aware of while designing your application or deploying it to an unknown server.

Whenever something depends on the type of interface, make sure your check is conclusive. Especially when you want to distinguish the command line interface (CLI) from the common gateway interface (CGI).

Note, that the php-cgi binary can be called from the command line, from a shell script or as a cron job as well! If so, the php_sapi_name() will always return the same value (i.e. "cgi-fcgi") instead of "cli" which you could expect.

Bad things happen to good people. Do not always expect /usr/bin/php to be a link to php-cli binary.

Luckily the contents of the $_SERVER and the $_ENV superglobal arrays depends on whether the php-cgi binary is called from the command line interface (by a shell script, by the cron, etc.) or by some HTTP server (i.e. lighttpd).

<?php
var_dump
($_SERVER);
?>

Try to call php-cgi binary from the command line interface and then via HTTP request and compare the output of the script above. There will be plenty options to satisfy almost everyone.

For the sake of security remember, that contents of the $_SERVER and the $_ENV superglobal arrays (as well as $_GET, $_POST, $_COOKIE, $_FILES and $_REQUEST) should be considered tainted.
up
26
hajo-p
10 years ago
some not yet mentioned sapi names:

cli-server -> php built-in webserver
srv -> hhvm
up
-2
michael at butlerpc dot net
3 years ago
When using nginx-unit with PHP, the sapi name seems to be "cli-server"
up
-30
anonymous
8 years ago
php_sapi_name (which is equal to PHP_SAPI) can either be cgi or any other cgi mod (dependent on which mod you are using)-

Examples:
'fpm-fcgi'
'cgi-fcgi'
[...]
To Top