php文件包含目錄配置open_basedir的使用與性能詳解
1.open_basedir介紹
open_basedir 將php所能打開(kāi)的文件限制在指定的目錄樹(shù)中,包括文件本身。當(dāng)程序要使用例如fopen()或file_get_contents()打開(kāi)一個(gè)文件時(shí),這個(gè)文件的位置將會(huì)被檢查。當(dāng)文件在指定的目錄樹(shù)之外,程序?qū)⒕芙^打開(kāi)。
本指令不受安全模式打開(kāi)或關(guān)閉的影響。
2.open_basedir設(shè)置方法
1.在php.ini 加入
open_basedir="指定目錄"
2.在程序中使用
ini_set('open_basedir', '指定目錄');
但不建議使用這種方法
3.在apache的httpd.conf中的Directory配置
php_admin_value open_basedir "指定目錄"
httpd.conf中的VritualHost
php_admin_value open_basedir "指定目錄"
4.nginx fastcgi.conf
fastcgi_param PHP_VALUE "open_basedir=指定目錄"
用open_basedir指定的限制實(shí)際上是前綴,不是目錄名。
也就是說(shuō) open_basedir=/home/fdipzone 也會(huì)允許訪問(wèn)/home/fdipzone_abc,如果要將訪問(wèn)限制為目錄,請(qǐng)使用斜線(xiàn)結(jié)束路徑名,例如:open_basedir=”/home/fdipzone/”
如果要設(shè)置多個(gè)目錄,window使用;分隔目錄,linux使用:分隔目錄。
3.使用open_basedir限制目錄訪問(wèn)
首先創(chuàng)建一個(gè)VirtualHost,
設(shè)置open_basedir 為/home/fdipzone/sites/in.fdipzone.com/
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/fdipzone/sites/in.fdipzone.com
ServerName in.fdipzone.com
php_admin_value open_basedir "/home/fdipzone/sites/in.fdipzone.com/"
<Directory "/home/fdipzone/sites/in.fdipzone.com">
allow from all Options + Indexes
</Directory>
</VirtualHost>
在上一層目錄 /home/fdipzone/sites/ 中創(chuàng)建一個(gè)test.txt文件,在in.fdipzone.com中創(chuàng)建php執(zhí)行以下代碼
<?php
echo file_get_contents('../test.txt');
?>
因?yàn)閠est.txt不在限定的目錄范圍內(nèi),因此php提示警告
Warning: file_get_contents(): open_basedir restriction in effect. File(../test.txt) is not within the allowed path(s): (/home/fdipzone/sites/in.fdipzone.com/) in /home/fdipzone/sites/in.fdipzone.com/index.php on line 3
4.設(shè)置open_basedir的性能分析
open_basedir開(kāi)啟后會(huì)影響I/O,因?yàn)槊總€(gè)調(diào)用的文件都需要判斷是否在限制目錄內(nèi)。
測(cè)試程序,讀取限制目錄內(nèi)同一文件10000次
<?php
// 記錄開(kāi)始時(shí)間
$starttime = getMicrotime();
// 讀取10000次文件
for($i=0; $i<10000; $i++){
file_get_contents('test.txt');
}
// 記錄結(jié)束時(shí)間
$endtime = getMicrotime();
printf("run time %f ms\r\n", ((float)($endtime)-(float)($starttime))*1000);
function getMicrotime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
?>
關(guān)閉open_basedir測(cè)試
run time 137.237072 ms
打開(kāi)open_basedir測(cè)試
run time 404.207945 ms
開(kāi)啟open_basedir后,執(zhí)行時(shí)間是關(guān)閉的3倍。
總結(jié):使用open_basedir可以限制程序可操作的目錄和文件,提高系統(tǒng)安全性。但會(huì)影響I/O性能導(dǎo)致系統(tǒng)執(zhí)行變慢,因此需要根據(jù)具體需求,在安全與性能上做平衡。
以上這篇php文件包含目錄配置open_basedir的使用與性能詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
PHP基于MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)象持久層的方法
這篇文章主要介紹了PHP基于MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)象持久層的方法,實(shí)例分析了php實(shí)現(xiàn)持久層的相關(guān)技巧,需要的朋友可以參考下2015-06-06
PHP+MySQL存儲(chǔ)數(shù)據(jù)常見(jiàn)中文亂碼問(wèn)題小結(jié)
這篇文章主要介紹了PHP+MySQL存儲(chǔ)數(shù)據(jù)常見(jiàn)中文亂碼問(wèn)題,針對(duì)php+mysql常見(jiàn)的中文亂碼問(wèn)題予以總結(jié)分析,并給出了解決方法供大家參考,需要的朋友可以參考下2016-06-06
PHP 函數(shù)call_user_func和call_user_func_array用法詳解
下面來(lái)和大家分享一下這個(gè)call_user_func_array和call_user_func函數(shù)的用法,另外附贈(zèng)func_get_args()函數(shù)和func_num_args()函數(shù),嘿嘿!!2014-03-03
php封裝的表單驗(yàn)證類(lèi)完整實(shí)例
這篇文章主要介紹了php封裝的表單驗(yàn)證類(lèi),結(jié)合完整實(shí)例形式分析了php針對(duì)表單元素正則驗(yàn)證與類(lèi)型判定的相關(guān)操作技巧,對(duì)于php程序設(shè)計(jì)的安全性有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10

