首页 >php教程 >PHP源码 >php5 通用兼容的公共文件.

php5 通用兼容的公共文件.

PHP中文网
PHP中文网原创
2016-05-25 17:01:421217浏览

每次新项目, 都要为环境配置做许多php的修正, 为此, 我整理了这一份通用的php抬头文件. 
使用方法, 将代码保存为某个php文件, 然后在项目中引入即可. 

代码作用如下: 
  1: 规范化编码问题, utf-8, 时间区域, 数字长度都有变更. 
  2: 规范化字符转义问题, 全部采用php5.3之后的标准, 都不转化. 
  3: session, gzip做判断启动. 这有利于防止隐性冲突. 
  4: 对SERVER数组进行整理, 隐藏掉一些非安全段, 增加以HTTP_开头的一些数据, 如内存占用, 起始时间, 当前url地址, ip, get, post等等. 你只要打印出$_SERVER数组, 基本上许多数据就都有了, 并且它是超全局的.

<?php
# 基础抬头 其中第三项释放的信息在浏览器debug时可见.
header(&#39;Content-language: zh&#39;); 
header(&#39;Content-type: text/html; charset=utf-8&#39;);
header(&#39;X-Powered-By: PHP/-&#39;.phpversion());
# 设置php文件永远不缓存. 可以在后面进行叠加影响的.
header(&#39;Pragma: no-cache&#39;);
header(&#39;Cache-Control: private&#39;,false); // required for certain browsers 
header(&#39;Cache-Control: no-cache, no-store, max-age=0, must-revalidate&#39;); 
header(&#39;Expires: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;) . &#39; GMT&#39;);
header(&#39;Last-Modified: &#39; . gmdate(&#39;D, d M Y H:i:s&#39;) . &#39; GMT&#39;);
# 设置一个结束时调用的函数. 请自行修改函数名.
function_exists(&#39;register_shutdown_function&#39;) && register_shutdown_function(&#39;ini_end&#39;);
# 设置执行时间,内部字符编码.
set_time_limit($set_time = 3600);
mb_internal_encoding(&#39;utf-8&#39;);
# 核心设置
ini_sets(&#39;session.name&#39;,&#39;PHPSESSID&#39;);
ini_sets(&#39;max_execution_time&#39;,$set_time);
ini_sets(&#39;max_input_time&#39;,$set_time);
ini_sets(&#39;zend.ze1_compatibility_mode&#39;, false);
ini_sets(&#39;precision&#39;, 72);
ini_sets(&#39;session.gc_maxlifetime&#39;,3600); //设置垃圾回收最大生存时间
ini_sets(&#39;session.gc_probability&#39;,30); //和session.gc_divisor一起构成清除垃圾的执行几率
ini_sets(&#39;session.gc_divisor&#39;,100);
ini_sets(&#39;date.timezone&#39;,&#39;UTC&#39;);
ini_sets(&#39;display_errors&#39;, &#39;Off&#39;);
error_reporting(E_ALL & ~E_NOTICE);
ini_http_server(); // this href
# 当$_SESSION数组为空时, 才初始化session.
if (!isset($_SESSION)) {
session_start();
}
# 当gzip没有自动启动时, 才启动gzip压缩.
if (!ini_get(&#39;zlib.output_compression&#39;)){
!ob_start(&#39;ob_gzhandler&#39;)?ob_start():ob_start(&#39;ob_gzhandler&#39;);
}
# 判断对引入字符的转入判断. 都设置为假.
if (version_compare(PHP_VERSION, &#39;5.3.0&#39;, &#39;<&#39;) && function_exists(&#39;set_magic_quotes_runtime&#39;)) {
set_magic_quotes_runtime(false);
}
# 对get post, cookie数组进行反转义工作, 以便让其与php5.3+兼容. 得到的数据都是没有转义的.
if(( function_exists(&#39;get_magic_quotes_gpc&#39;) && get_magic_quotes_gpc())
 || ( ini_get(&#39;magic_quotes_sybase&#39;) && ( strtolower(ini_get(&#39;magic_quotes_sybase&#39;)) != &#39;off&#39;))){
 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[&#39;MIBDIRS&#39;],$_SERVER[&#39;MYSQL_HOME&#39;],$_SERVER[&#39;OPENSSL_CONF&#39;],$_SERVER[&#39;PHP_PEAR_SYSCONF_DIR&#39;],$_SERVER[&#39;PHPRC&#39;],$_SERVER[&#39;SystemRoot&#39;],$_SERVER[&#39;COMSPEC&#39;],$_SERVER[&#39;PATHEXT&#39;], $_SERVER[&#39;WINDIR&#39;],$_SERVER[&#39;PATH&#39;]);
# 一些函数.
function ini_end(){}
function ini_sets($key, $val){return @ini_set($key, $val);}
function ini_http_server(){
 if (!$_SERVER[&#39;REQUEST_URI&#39;]) { // IIS 5 compatibility
 $_SERVER[&#39;REQUEST_URI&#39;] = $_SERVER[&#39;ORIG_PATH_INFO&#39;];
}
 if (!strpos($_SERVER[&#39;REQUEST_URI&#39;], &#39;?&#39;) && $_SERVER[&#39;QUERY_STRING&#39;] != &#39;&#39;) { // IIS 7 compatibility
 $_SERVER[&#39;REQUEST_URI&#39;] .= &#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];
}
 $_SERVER[&#39;REQUEST_URI&#39;] = strtr($_SERVER[&#39;REQUEST_URI&#39;], array(&#39;&&&#39;=>&#39;&&#39;));
 $HTTPS = $_SERVER[&#39;HTTPS&#39;] && strcasecmp($_SERVER[&#39;HTTPS&#39;], &#39;off&#39;);
 $php_selfs = rawurlencode(dirname($_SERVER[&#39;SCRIPT_NAME&#39;]));
 $php_selfs = strtr($php_selfs, array(&#39;%2F&#39;=>&#39;/&#39;,&#39;%5C&#39;=>&#39;/&#39;));
 $php_selfs = trim($php_selfs, &#39;/&#39;);
if($php_selfs)
 $_SERVER[&#39;REQUEST_URI&#39;] = strtr($_SERVER[&#39;REQUEST_URI&#39;], array($php_selfs.&#39;/&#39;=>&#39;&#39;));
 $httppre = (!$HTTPS)?&#39;http&#39;:&#39;https&#39;;
 if($_SERVER[&#39;SERVER_PORT&#39;] !== &#39;80&#39; && $_SERVER[&#39;SERVER_PORT&#39;] !== &#39;443&#39;){
 $host = trim($httppre.&#39;://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].&#39;:&#39;.$_SERVER[&#39;SERVER_PORT&#39;].&#39;/&#39;.$php_selfs,&#39;/&#39;).&#39;/&#39;;
}else{
 $php_selfs && $php_selfs =&#39;/&#39;.$php_selfs;
 $host = trim($httppre.&#39;://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$php_selfs,&#39;/&#39;).&#39;/&#39;;
}
 $files = trim($_SERVER[&#39;REQUEST_URI&#39;], &#39;./&#39;);
 $_SERVER[&#39;HTTP_URL&#39;] = $host.$files;
 $_SERVER[&#39;HTTP_PATH&#39;] = $_SERVER[&#39;DOCUMENT_ROOT&#39;].&#39;/&#39;;
 $_SERVER[&#39;HTTP_IP&#39;] = max(getenv(&#39;SERVER_ADDR&#39;),getenv(&#39;REMOTE_ADDR&#39;),getenv(&#39;HTTP_X_FORWARDED_FOR&#39;),getenv(&#39;HTTP_CLIENT_IP&#39;));
 $_SERVER[&#39;HTTP_TIME_FLOAT&#39;] = microtime(true);
 $_SERVER[&#39;HTTP_MEMORY_PEAK_USAGE&#39;] = memory_get_peak_usage();
 $_SERVER[&#39;HTTP_MEMORY_USAGE&#39;] = memory_get_usage();
 $_SERVER[&#39;HTTP_GET&#39;] = $_GET;
 $_SERVER[&#39;HTTP_POST&#39;] = $_POST;
 $_SERVER[&#39;HTTP_COOKIE&#39;] = $_COOKIE;
}


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn