2013-02-26 58 views
0

存在以下問題:我必須檢測FastCGI SAPI才能發送自定義響應。對於FastCGI,它看起來像是'未找到狀態404',而不是'HTTP/1.1 404 Not Found'。就我所知,PHP僅將FastCGI作爲FPM的一部分來實現。使用PHP檢測FastCGI SAPI

因此,它是檢測FastCGI的正確方法:

 if (preg_match('|fpm|', PHP_SAPI)) 
     print('FastCGI'); 
+0

http://stackoverflow.com/questions/609044/how-to-know-for-sure-if-fastcgi-is-being-used-to-run-php-scripts – 2013-02-26 08:31:37

+1

您確定需要發送自定義響應?請參閱[this stackoverflow post](http://stackoverflow.com/questions/8828275/still-necessary-to-use-status-404-not-found-for-fcgi)瞭解更多關於HTTP vs Status和FCGI的信息 – alldayremix 2013-02-26 08:35:18

+0

Yeah ,可能這就是我需要的。 – 2013-02-26 09:22:28

回答

0

我會用

if (PHP_SAPI === 'fpm-fcgi') { 
    print 'FastCGI'; 
} 

如果你想檢測到任何CGI /快CGI環境,我會用

if (preg_match('|cgi|', PHP_SAPI)) { 
    print('FastCGI'); 
} 

我避免使用正則表達式,僅僅是因爲它傾向於使三年後在我後面的人試圖閱讀代碼追蹤一些無關的錯誤。