日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

基于magic_quotes_gpc與magic_quotes_runtime的區(qū)別與使用介紹_PHP教程

編輯Tag賺U幣

推薦:『PHP』PHP截斷函數(shù)mb_substr()使用介紹
截斷文章標題,控制在15個文字,接下來為大家講解下如何實現(xiàn)這個需求,感興趣的朋友可以參考下哈

當你的數(shù)據(jù)中有一些 \ ” ‘
這樣的字符要寫入到數(shù)據(jù)庫里面,又想不被過濾掉的時候,它就很有用,會在這些字符前自動加上\,如
中國\地大物博”哈哈”
中國\\地大物博\”哈哈\”
可以使用set_maginc_quotes_runtime(0)關(guān)閉掉,當然你也可以直接在php.ini中設置。
get_magic_quotes_runtime() 取得 PHP 環(huán)境變量 magic_quotes_runtime 的值。

magic_quotes_gpc 為 on,它主要是對所有的 GET、POST 和 COOKIE 數(shù)據(jù)自動運行 addslashes()。不要對已經(jīng)被 magic_quotes_gpc 轉(zhuǎn)義過的字符串使用 addslashes(),因為這樣會導致雙層轉(zhuǎn)義。遇到這種情況時可以使用函數(shù) get_magic_quotes_gpc() 進行檢測。

兩者不同

set_magic_quotes_runtime() 可以讓程序員在代碼中動態(tài)開啟或關(guān)閉 magic_quotes_runtime,
set_magic_quotes_runtime(1) 表示開啟,set_magic_quotes_runtime(0) 則表示關(guān)閉。當set_magic_quotes_runtime(1) 時,從數(shù)據(jù)庫或通過fread之類的函數(shù)讀取的文本,將自動對' “和\自動加上反斜杠\進行轉(zhuǎn)義,防止溢出。這在對數(shù)據(jù)庫的數(shù)據(jù)進行轉(zhuǎn)移的時候非常有用。但在一般情況下,應當將其關(guān)閉,否則從數(shù)據(jù)庫讀取出來的數(shù)據(jù)單引號、雙引號和反斜杠都會被加上\,導致顯示不正常。像Discuz,PHPWind都在公共文件的頭部加上一句 set_magic_quotes_runtime(0); 強制關(guān)閉 magic_quotes_runtime 。

magic_quotes_gpc

作用范圍是:WEB客戶服務端;
作用時間:請求開始是,例如當腳本運行時.

magic_quotes_runtime

作用范圍:從文件中讀取的數(shù)據(jù)或執(zhí)行exec()的結(jié)果或是從SQL查詢中得到的;
作用時間:每次當腳本訪問運行狀態(tài)中產(chǎn)生的數(shù)據(jù).

所以

magic_quotes_gpc的設定值將會影響通過Get/Post/Cookies獲得的數(shù)據(jù),
magic_quotes_runtime的設定值將會影響從文件中讀取的數(shù)據(jù)或從數(shù)據(jù)庫查詢得到的數(shù)據(jù),
magic_quotes_gpc 是對通過GET、POST、COOKIE傳遞的數(shù)據(jù)進行轉(zhuǎn)義,一般在數(shù)據(jù)入庫前要先進行轉(zhuǎn)義,
magic_quotes_gpc不能在代碼中動態(tài)開啟或關(guān)閉,需要到php.ini將magic_quotes_gpc設置為on或off,
代碼中可以用get_magic_quotes_gpc獲取magic_quotes_gpc的狀態(tài)。
當magic_quotes_gpc為off時,需要手工對數(shù)據(jù)進行addslashes,代碼如下:

復制代碼 代碼如下:www.hl5o.cn

if (!get_magic_quotes_gpc()) {
new_addslashes($_GET);
new_addslashes($_POST);
new_addslashes($_COOKIE);
}

function new_addslashes($string) {
if (is_array($string)) {
foreach ($string as $key => $value) {
$string[$key] = new_addslashes($value);
}
} else {
$string = addslashes($string);
}
return $string;
}

另一示例:
復制代碼 代碼如下:www.hl5o.cn

$data1 = $_POST['aaa'];
$data2 = implode(file('1.txt'));

if (get_magic_quotes_gpc()) {
//把數(shù)據(jù)$data1直接寫入數(shù)據(jù)庫
} else {
$data1 = addslashes($data1);
//把數(shù)據(jù)$data1寫入數(shù)據(jù)庫
}

if (get_magic_quotes_runtime()){
//把數(shù)據(jù)$data2直接寫入數(shù)據(jù)庫
//從數(shù)據(jù)庫讀出的數(shù)據(jù)要經(jīng)過一次stripslashes()之后輸出
} else {
$data2 = addslashes($data2);
//把數(shù)據(jù)$data2寫入數(shù)據(jù)庫
//從數(shù)據(jù)庫讀出的數(shù)據(jù)直接輸出
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++

經(jīng)驗總結(jié):

一、對于GPC,不管系統(tǒng)有沒有開啟magic_quotes_gpc(即php.ini中magic_quotes_gpc = On),我們統(tǒng)一開啟 magic_quotes_gpc,對get、post、cookie的內(nèi)容進行轉(zhuǎn)義。操作如下:
(摘自uchome系統(tǒng))

復制代碼 代碼如下:www.hl5o.cn

function saddslashes($string) {
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}

//GPC過濾
$magic_quote = get_magic_quotes_gpc();
if(empty($magic_quote)) {
$_GET = saddslashes($_GET);
$_POST = saddslashes($_POST);
}

//COOKIE,給cookie值轉(zhuǎn)義
$prelength = strlen($_SC['cookiepre']);
foreach ($_COOKIE as $key => $val) {
if(substr($key, 0, $prelength) == $_SC['cookiepre']) {
$_SCOOKIE[(substr($key, $prelength))] = empty($magic_quote) ? saddslashes($val) : $val;
}
}

二、對于magic_quotes_runtime,我們統(tǒng)一關(guān)閉它,即set_magic_quotes_runtime(0);不讓從數(shù)據(jù)庫讀取出來的數(shù)據(jù)的單引號、雙引號和反斜杠都自動被加上\。這樣,對數(shù)據(jù)庫的操作如下:添加數(shù)據(jù)到數(shù)據(jù)庫之前,我們手動對數(shù)據(jù)進行addslashes(),而從數(shù)據(jù)庫取出數(shù)據(jù)時,則作相反操作,即stripslashes()。

三、對于要序列化的內(nèi)容,要保持裸數(shù)據(jù),即要去掉轉(zhuǎn)義,stripslashes(),然后在把序列化過的內(nèi)容保存到數(shù)據(jù)庫當中(注意,序列化過的內(nèi)容是不帶單引號(')、雙引號(”)、反斜線(\)的),示例如下:
$feedarr['body_data'] = serialize(stripslashes($body_data));

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

出現(xiàn)Function set_magic_quotes_runtime() is deprecated 問題?

在安裝PHPCMS出現(xiàn)Deprecated: Function set_magic_quotes_runtime() is deprecated 錯誤,查了一下網(wǎng)絡及資料發(fā)現(xiàn)是PHP5.3和PHP6.0之后移除了set_magic_quotes_runtime()函數(shù)。
我可以使用如下方案替代:

view sourceprint?
@set_magic_quotes_runtime(0);

view sourceprint?
ini_set("magic_quotes_runtime", 0);

view sourceprint?
if (phpversion() < '5.3.0') {
set_magic_quotes_runtime(0);
}

分享:基于PHP Web開發(fā)MVC框架的Smarty使用說明
本篇文章小編為大家介紹,基于PHP Web開發(fā)MVC框架的Smarty使用說明。需要的朋友參考下

來源:模板無憂//所屬分類:PHP教程/更新時間:2013-04-23
相關(guān)PHP教程