《PHP應用:php文件包含目錄配置open_basedir的使用與性能詳解》要點:
本文介紹了PHP應用:php文件包含目錄配置open_basedir的使用與性能詳解,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學習1.open_basedir介紹
PHP學習open_basedir 將php所能打開的文件限制在指定的目錄樹中,包括文件本身.當程序要使用例如fopen()或file_get_contents()打開一個文件時,這個文件的位置將會被檢查.當文件在指定的目錄樹之外,程序?qū)⒕芙^打開.
PHP學習本指令不受安全模式打開或關(guān)閉的影響.
PHP學習2.open_basedir設置方法
PHP學習1.在php.ini 加入
PHP學習open_basedir="指定目錄"
PHP學習2.在程序中使用
PHP學習ini_set('open_basedir', '指定目錄');
PHP學習但不建議使用這種方法
PHP學習3.在apache的httpd.conf中的Directory配置
PHP學習php_admin_value open_basedir "指定目錄"
httpd.conf中的VritualHost
PHP學習php_admin_value open_basedir "指定目錄"
PHP學習4.nginx fastcgi.conf
PHP學習fastcgi_param PHP_VALUE "open_basedir=指定目錄"
PHP學習用open_basedir指定的限制實際上是前綴,不是目錄名.
PHP學習也就是說 open_basedir=/home/fdipzone 也會允許訪問/home/fdipzone_abc,如果要將訪問限制為目錄,請使用斜線結(jié)束路徑名,例如:open_basedir=”/home/fdipzone/”
PHP學習如果要設置多個目錄,window使用;分隔目錄,linux使用:分隔目錄.
PHP學習3.使用open_basedir限制目錄訪問
PHP學習首先創(chuàng)建一個VirtualHost,
PHP學習設置open_basedir 為/home/fdipzone/sites/in.fdipzone.com/
PHP學習
<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>
PHP學習在上一層目錄 /home/fdipzone/sites/ 中創(chuàng)建一個test.txt文件,在in.fdipzone.com中創(chuàng)建php執(zhí)行以下代碼
PHP學習
<?php
echo file_get_contents('../test.txt');
?>
PHP學習因為test.txt不在限定的目錄范圍內(nèi),因此php提示警告
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
PHP學習4.設置open_basedir的性能分析
PHP學習open_basedir開啟后會影響I/O,因為每個調(diào)用的文件都需要判斷是否在限制目錄內(nèi).
PHP學習測試程序,讀取限制目錄內(nèi)同一文件10000次
PHP學習
<?php
// 記錄開始時間
$starttime = getMicrotime();
// 讀取10000次文件
for($i=0; $i<10000; $i++){
file_get_contents('test.txt');
}
// 記錄結(jié)束時間
$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;
}
?>
PHP學習關(guān)閉open_basedir測試
PHP學習run time 137.237072 ms
PHP學習打開open_basedir測試
PHP學習run time 404.207945 ms
PHP學習開啟open_basedir后,執(zhí)行時間是關(guān)閉的3倍.
PHP學習總結(jié):使用open_basedir可以限制程序可操作的目錄和文件,提高系統(tǒng)安全性.但會影響I/O性能導致系統(tǒng)執(zhí)行變慢,因此需要根據(jù)具體需求,在安全與性能上做平衡.
PHP學習以上這篇php文件包含目錄配置open_basedir的使用與性能詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持維易PHP.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/1282.html