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

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

該文件的作用如下:

1: 規(guī)范化編碼問(wèn)題, utf-8, 時(shí)間區(qū)域, 數(shù)字長(zhǎng)度都有變更.

2: 規(guī)范化字符轉(zhuǎn)義問(wèn)題, 全部采用php5.3之后的標(biāo)準(zhǔn), 都不轉(zhuǎn)化.

3: session, gzip做判斷啟動(dòng). 這有利于防止隱性沖突.

4: 對(duì)SERVER數(shù)組進(jìn)行整理, 隱藏掉一些非安全段, 增加以HTTP_開(kāi)頭的一些數(shù)據(jù), 如內(nèi)存占用, 起始時(shí)間, 當(dāng)前url地址, ip, get, post等等. 你只要打印出$_SERVER數(shù)組, 基本上許多數(shù)據(jù)就都有了, 并且它是超全局的.

<?php

# 基礎(chǔ)抬頭 其中第三項(xiàng)釋放的信息在瀏覽器debug時(shí)可見(jiàn).

header('Content-language: zh');

header('Content-type: text/html; charset=utf-8');

header('X-Powered-By: PHP/-'.phpversion());

# 設(shè)置php文件永遠(yuǎn)不緩存. 可以在后面進(jìn)行疊加影響的.

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');

# 設(shè)置一個(gè)結(jié)束時(shí)調(diào)用的函數(shù). 請(qǐng)自行修改函數(shù)名.

function_exists('register_shutdown_function') && register_shutdown_function('ini_end');

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

set_time_limit($set_time = 3600);

mb_internal_encoding('utf-8');

# 核心設(shè)置

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); //設(shè)置垃圾回收最大生存時(shí)間

ini_sets('session.gc_probability',30); //和session.gc_divisor一起構(gòu)成清除垃圾的執(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

# 當(dāng)$_SESSION數(shù)組為空時(shí), 才初始化session.

if (!isset($_SESSION)) {

session_start();

}

# 當(dāng)gzip沒(méi)有自動(dòng)啟動(dòng)時(shí), 才啟動(dòng)gzip壓縮.

if (!ini_get('zlib.output_compression')){

!ob_start('ob_gzhandler')?ob_start():ob_start('ob_gzhandler');

}

# 判斷對(duì)引入字符的轉(zhuǎn)入判斷. 都設(shè)置為假.

if (version_compare(PHP_VERSION, '5.3.0', '<') && function_exists('set_magic_quotes_runtime')) {

set_magic_quotes_runtime(false);

}

# 對(duì)get post, cookie數(shù)組進(jìn)行反轉(zhuǎn)義工作, 以便讓其與php5.3+兼容. 得到的數(shù)據(jù)都是沒(méi)有轉(zhuǎn)義的.

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數(shù)組, 釋放掉$_SERVER數(shù)組中幾個(gè)關(guān)鍵性數(shù)值.

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']);

# 一些函數(shù).

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;

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php5通用兼容的公共文件
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)