在有的PHP中定义ENVIRONMENT常量,可以区分开发环境和生产环境,如:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
switch (ENVIRONMENT)
{
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
break;
case 'testing':
case 'production':
ini_set('display_errors', 0);
if (version_compare(PHP_VERSION, '5.3', '>='))
{
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
}
else
{
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
}
break;
default:
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
echo 'The application environment is not set correctly.';
exit(1); // EXIT_ERROR
}
那么这里的$_SERVER['CI_ENV']如果要定义在哪里定义呢?
Nginx配置设置$_SERVER变量
打开一个nginx的网站配置:
location ~ \.php$ {
try_files $uri =404;
root /usr/share/nginx/html/tanteng.me/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MYENV 'DEV';
include fastcgi_params;
}
这里的fastcgi_param MYENV ‘DEV’,就是新加的一行语句。
在PHP中打印$_SERVER看看:
……
"SERVER_ADDR" => "192.168.10.31"
"SERVER_NAME" => "www.tanteng.me"
"SERVER_PORT" => "80"
"SERVER_SOFTWARE" => "nginx/1.9.11"
"SERVER_PROTOCOL" => "HTTP/1.1"
"SERVER_ADMIN" => ""
"SERVER_SIGNATURE" => ""
"SCRIPT_NAME" => "/index.php"
"REDIRECT_STATUS" => "200"
"REQUEST_URI" => "/test/redis"
"REMOTE_PORT" => 61290
"DOCUMENT_URI" => "/index.php"
"REQUEST_METHOD" => "GET"
"REMOTE_ADDR" => "192.168.10.1"
"CONTENT_TYPE" => ""
"DOCUMENT_ROOT" => "/usr/share/nginx/html/tanteng.me/public"
"MYENV" => "DEV"
"CONTENT_LENGTH" => ""
"QUERY_STRING" => ""
"SCRIPT_FILENAME" => "/usr/share/nginx/html/tanteng.me/public/index.php"
"SCRIPT_URL" => "/index.php"
"SCRIPT_URI" => http://www.111cn.net
"PHP_SELF" => "/index.php"
……
就这样就可以看到多了一个”MYENV”的变量。