php5通用兼容的公共文件
來源:易賢網 閱讀:1181 次 日期:2015-04-15 15:17:19
溫馨提示:易賢網小編為您整理了“php5通用兼容的公共文件”,方便廣大網友查閱!

每次新項目, 都要為環(huán)境配置做許多php的修正, 為此, 我整理了這一份通用的php抬頭文件. 使用方法, 將代碼保存為某個php文件, 然后在項目中引入即可.

該文件的作用如下:

1: 規(guī)范化編碼問題, utf-8, 時間區(qū)域, 數字長度都有變更.

2: 規(guī)范化字符轉義問題, 全部采用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');

# 設置執(zhí)行時間,內部字符編碼.

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一起構成清除垃圾的執(zhí)行幾率

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;

}

更多信息請查看IT技術專欄

更多信息請查看網絡編程
易賢網手機網站地址:php5通用兼容的公共文件
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網