Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以读取包含文件内容的php文件吗?_Php_Security_Include_File Get Contents_Include Path - Fatal编程技术网

可以读取包含文件内容的php文件吗?

可以读取包含文件内容的php文件吗?,php,security,include,file-get-contents,include-path,Php,Security,Include,File Get Contents,Include Path,我创建了加载的网页(只是css,js)从url获取请求。我以前用包含做过这件事,但这是危险的,因为黑客可以包含不是来自公共html的文件。我在文件获取内容上试过这件事,它说请求不好,这很完美,但我想确定,因为实际上我听说文件获取内容s是危险?我不想帮助黑客读取php文件,因为我听说可以使用file\u get\u contents?file\u get\u contents()。然后,恶意用户可能会使用例如/etc/passwd来读取服务器的所有用户名。或者,他/她可以使用“..”作为文件名的一

我创建了加载的网页(只是
css,js
)从url获取请求。我以前用
包含
做过这件事,但这是危险的,因为黑客可以包含不是来自
公共html
的文件。我在
文件获取内容
上试过这件事,它说请求不好,这很完美,但我想确定,因为实际上我听说
文件获取内容
s是危险?我不想帮助黑客读取php文件,因为我听说可以使用
file\u get\u contents

file\u get\u contents()。然后,恶意用户可能会使用例如
/etc/passwd
来读取服务器的所有用户名。或者,他/她可以使用“..”作为文件名的一部分来访问文件,即使在文件名前面加上允许的路径(例如“../../etc/passwd”)

为了防止这种情况发生,您应该始终检查用户给定的值的有效性,例如,文件名或路径是否在文件系统的预期部分,因此只能访问您希望用户访问的文件。PHP有一个函数可以使相对路径(或包含符号链接的路径)成为“真实的”:

将路径设置为“真实”后,您可以检查路径是否位于文件系统的预期部分,然后将其安全地用于
文件\u get\u contents()


只要授予权限,您就可以读取任何包含
file\u get\u内容的文件。您应该使用白名单与请求的文件进行比较,以确保,只有你想被读取的文件才会被外传。好吧,这是可行的,但我想确定是否有其他方法可以跟踪文件内容并读取不在公共html中的文件?此外,这种方法在本地主机上不起作用,因为我回显$real它是E:\wamp64\www\website\public\css\font\google\u font.css,但它应该是www/website/(不同的是/和)。这可能是问题吗?我可以像(trim($real)!=''{}那样检查它吗?因为如果文件包含,它将返回空的../它总是真的吗?
$wanted = $_GET['path'];
$real = realpath('/var/www/user/accessible/' . $wanted);
if (strpos($real, '/var/www/user/accessible/') === 0) {
   echo file_get_contents($real);
} else {
   throw new Exception('not allowed to access this file');
}