drh37116 2013-12-03 14:31
浏览 14
已采纳

包括安全 - 相对和绝对路径

I wanted to know if it is safe to use includes on pages.

I read using allow_url_include is un-safe, I was using it before with absolute paths, but worked out I could bypass the problem with relative paths, but is this really safe?

Also would something like this really work?

 <?php 
   $header= preg_replace('/[^a-zA-Z0-9_]/', '', $_GET['header']);
   include "http://mysite.co.uk/directory/$header.php"; 
  ?>
  • 写回答

1条回答 默认 最新

  • duanlushen8940 2013-12-03 14:40
    关注

    Here's an example that illustrates the concept I made in the comments above.

    $headers = array('loggedin.php','loggedout.php','someotherheader.php', 'etc.php');
    $key     = (int) $_GET['header']; // We know it must be a integer so cast to int
    $header  = $headers[$key];
    include "http://mysite.co.uk/directory/$header"; 
    

    This can be improved upon by verifying the key exists in the array and if it doesn't defaulting to a default header. You also shouldn't be including files via URL. You should be using the path to the file on disk. It's much faster.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用