文件存在安全吗?

3 php security

if (file_exists("pages/$page.php")) {
  include($page.'.php');
}
Run Code Online (Sandbox Code Playgroud)

这样安全吗?

使用Safe我的意思是你不能包括远程脚本等

St.*_*son 7

当然不是,特别是如果 $page = "./configuration"

我建议用这样的东西替换它:

$pages = array("blog", "home", "login");
if (in_array(strtolower($page), $pages))
  include("pages/$page.php");
Run Code Online (Sandbox Code Playgroud)

编辑:您可以使用此代码生成可接受页面的列表.

$pages = array();
if ($dh = opendir("pages")) {
  while (($file = readdir($dh)) !== false) {
    if (strstr($file, ".php") !== false) // make sure it is a .php file
      $pages[] = substr($file, -4); // remove the .php
  }
  closedir($dh);
}
Run Code Online (Sandbox Code Playgroud)

  • 关键是,您要在数组中列出您的页面.因此,您可以限制您可以访问的内容.我来给你一些替代代码. (2认同)