每次新项目, 都要为环境配置做许多php的修正, 为此, 我整理了这一份通用的php抬头文件.
使用方法, 将代码保存为某个php文件, 然后在项目中引入即可.
代码作用如下:
1: 规范化编码问题, utf-8, 时间区域, 数字长度都有变更.
2: 规范化字符转义问题, 全部采用php5.3之后的标准, 都不转化.
3: session, gzip做判断启动. 这有利于防止隐性冲突.
4: 对SERVER数组进行整理, 隐藏掉一些非安全段, 增加以HTTP_开头的一些数据, 如内存占用, 起始时间, 当前url地址, ip, get, post等等. 你只要打印出$_SERVER数组, 基本上许多数据就都有了, 并且它是超全局的.
<?php # 基础抬头 其中第三项释放的信息在浏览器debug时可见. header('Content-language: zh'); header('Content-type: text/html; charset=utf-8'); header('X-Powered-By: PHP/-'.phpversion()); # 设置php文件永远不缓存. 可以在后面进行叠加影响的. header('Pragma: no-cache'); header('Cache-Control: private',false); // required for certain browsers header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Expires: '.gmdate('D, d M Y H:i:s') . ' GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); # 设置一个结束时调用的函数. 请自行修改函数名. function_exists('register_shutdown_function') && register_shutdown_function('ini_end'); # 设置执行时间,内部字符编码. set_time_limit($set_time = 3600); mb_internal_encoding('utf-8'); # 核心设置 ini_sets('session.name','PHPSESSID'); ini_sets('max_execution_time',$set_time); ini_sets('max_input_time',$set_time); ini_sets('zend.ze1_compatibility_mode', false); ini_sets('precision', 72); ini_sets('session.gc_maxlifetime',3600); //设置垃圾回收最大生存时间 ini_sets('session.gc_probability',30); //和session.gc_divisor一起构成清除垃圾的执行几率 ini_sets('session.gc_divisor',100); ini_sets('date.timezone','UTC'); ini_sets('display_errors', 'Off'); error_reporting(E_ALL & ~E_NOTICE); ini_http_server(); // this href # 当$_SESSION数组为空时, 才初始化session. if (!isset($_SESSION)) { session_start(); } # 当gzip没有自动启动时, 才启动gzip压缩. if (!ini_get('zlib.output_compression')){ !ob_start('ob_gzhandler')?ob_start():ob_start('ob_gzhandler'); } # 判断对引入字符的转入判断. 都设置为假. if (version_compare(PHP_VERSION, '5.3.0', '<') && function_exists('set_magic_quotes_runtime')) { set_magic_quotes_runtime(false); } # 对get post, cookie数组进行反转义工作, 以便让其与php5.3+兼容. 得到的数据都是没有转义的. if(( function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) || ( ini_get('magic_quotes_sybase') && ( strtolower(ini_get('magic_quotes_sybase')) != 'off'))){ foreach($_GET as $k => $v) $_GET[$k] = stripslashes($v); foreach($_POST as $k => $v) $_POST[$k] = stripslashes($v); foreach($_COOKIE as $k => $v) $_COOKIE[$k] = stripslashes($v); } # 清空$_ENV数组, 释放掉$_SERVER数组中几个关键性数值. unset($_ENV, $_SERVER['MIBDIRS'],$_SERVER['MYSQL_HOME'],$_SERVER['OPENSSL_CONF'],$_SERVER['PHP_PEAR_SYSCONF_DIR'],$_SERVER['PHPRC'],$_SERVER['SystemRoot'],$_SERVER['COMSPEC'],$_SERVER['PATHEXT'], $_SERVER['WINDIR'],$_SERVER['PATH']); # 一些函数. function ini_end(){} function ini_sets($key, $val){return @ini_set($key, $val);} function ini_http_server(){ if (!$_SERVER['REQUEST_URI']) { // IIS 5 compatibility $_SERVER['REQUEST_URI'] = $_SERVER['ORIG_PATH_INFO']; } if (!strpos($_SERVER['REQUEST_URI'], '?') && $_SERVER['QUERY_STRING'] != '') { // IIS 7 compatibility $_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING']; } $_SERVER['REQUEST_URI'] = strtr($_SERVER['REQUEST_URI'], array('&&'=>'&')); $HTTPS = $_SERVER['HTTPS'] && strcasecmp($_SERVER['HTTPS'], 'off'); $php_selfs = rawurlencode(dirname($_SERVER['SCRIPT_NAME'])); $php_selfs = strtr($php_selfs, array('%2F'=>'/','%5C'=>'/')); $php_selfs = trim($php_selfs, '/'); if($php_selfs) $_SERVER['REQUEST_URI'] = strtr($_SERVER['REQUEST_URI'], array($php_selfs.'/'=>'')); $httppre = (!$HTTPS)?'http':'https'; if($_SERVER['SERVER_PORT'] !== '80' && $_SERVER['SERVER_PORT'] !== '443'){ $host = trim($httppre.'://'.$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].'/'.$php_selfs,'/').'/'; }else{ $php_selfs && $php_selfs ='/'.$php_selfs; $host = trim($httppre.'://'.$_SERVER['HTTP_HOST'].$php_selfs,'/').'/'; } $files = trim($_SERVER['REQUEST_URI'], './'); $_SERVER['HTTP_URL'] = $host.$files; $_SERVER['HTTP_PATH'] = $_SERVER['DOCUMENT_ROOT'].'/'; $_SERVER['HTTP_IP'] = max(getenv('SERVER_ADDR'),getenv('REMOTE_ADDR'),getenv('HTTP_X_FORWARDED_FOR'),getenv('HTTP_CLIENT_IP')); $_SERVER['HTTP_TIME_FLOAT'] = microtime(true); $_SERVER['HTTP_MEMORY_PEAK_USAGE'] = memory_get_peak_usage(); $_SERVER['HTTP_MEMORY_USAGE'] = memory_get_usage(); $_SERVER['HTTP_GET'] = $_GET; $_SERVER['HTTP_POST'] = $_POST; $_SERVER['HTTP_COOKIE'] = $_COOKIE; }