【问题标题】:How to make php website more secure如何让php网站更安全
【发布时间】:2017-02-02 02:34:03
【问题描述】:

我找到了这个tutorial,但现在我什至无法访问我的 php 页面。

索引页面效果很好。我有通过 index.php 文件提取的初始页面:

<?php

    include ("home.html");

?>

效果很好。

但是当我尝试打开这个页面时:

<a href="our_projects.php" id='text'>

它会导致这个错误:

禁止

You don't have permission to access /our_projects.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

由于我添加了设置:

# In case there is no PHP, deny access to php files (for safety)
<IfModule !php5_module>
    <FilesMatch "\.(php|phtml)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</IfModule>
# the following should be added if you want to parse .php and .phtml file as PHP
# .phps will add syntax highlighting to the file when requesting it with a browser
<IfModule php5_module>
    AddType text/html .php .phtml .phps
    AddHandler application/x-httpd-php .php .phtml
    AddHandler application/x-httpd-php-source .phps
</IfModule>

我的问题是如何防止外部下载php文件,但同时允许浏览器显示内容。

我不希望有人读取 PHP 文件的主要原因是阻止读取一些安全数据,例如数据库密码等。

我还想放弃在地址行中显示 .php。我找到了an answer,但地址行的路径中仍然包含 .php。

【问题讨论】:

  • 可能只是因为您没有使用php5,而是使用php7。不管怎样,看看这里:stackoverflow.com/questions/13387978/… 答案不会阻止其他人获得您的代码,但会阻止他们获得敏感数据。
  • @icecub,谢谢,对不起,我不是 php 开发人员,所以可能需要先看一些教程。
  • 没问题。如果您以后需要更深入的帮助,请随时与我联系(电子邮件在我的个人资料中)。我很高兴能花几个小时和你一起教 php :)
  • @icecub,哦,真的!酷!)

标签: php


【解决方案1】:

我个人会说这样的安全问题是没有用的。在我建立的众多网站中。这些我都没用过。

据我所知,防止人们进入您不希望他们去的地方的最佳方法是将您的所有课程集中到一个文件夹中,并拒绝从外部访问该文件夹。然后,您使用根目录中的一个文件(index.php 左右)通过请求的 URL 调用所述文件。这也会为您提供用户友好的 URL。

一点点面向对象的编码应该可以解决大多数安全问题。

我会在这里给你一个这样的壮举的工作示例:

您的根文件:

.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#Request index.php and put a GET parameter 'request'
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]

然后,从您的 index.php 中,包含您的主类和路由

  require_once 'application/main.php';
  require_once 'application/routing.php';

您无法访问的文件:

将它们放在子目录中,例如 /application/ .htaccess:

#This will deny access from any outside source.
#You reach these files through index.php
deny from all

主类

 class main
  {
    public $someVar;

    public function setSomeVar(){
      $this->someVar = "I can get into the main class,
                        and shall use it for global functions!";
    }
}

请求的页面类:home.php

class home extends main
{
  public function renderTemplate(){
    //You can reach the main's functions from here
    //You could create a rendertemplate function in the main class
    //And set the template from this function for example.
     $this->setSomeVar();
     echo $this->someVar();
   }
}

然后,最重要的是,您将拥有自己的路由

  $noRequest = true;
  $params    = array();
  $class;
  //all routes go from your index.php's location
  $filepath = "application/";
  //Get the requested class and parameters
  $getRequest = explode('/', $request->get('request'));
  //Load homepage if no special request is made
  if( $getRequest[0] == '' ) {
    $page = "home";
  } else {
    //get the class
    $page = rtrim($getRequest[0], '/');
    //Get the called function
    $getFunction = isset( $getRequest[1] ) ? $getRequest[1] : false;
  }
  //Include the requested class. Otherwise, give back a 404
  if( file_exists($filepath . $page . ".php") ) {
    //include the class
    require_once $filepath . $page . ".php";
    //set the class object
    $class = new $page();
  } else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found", true, 404);
    //TODO:create 404 class
    echo "Page not found";
    exit();
  }

  //There is a function being called, go get it!
  if( $getFunction ) {
    //Make sure you've gotten the parameters as well
    $paramCount = count($getRequest);
    //skip 0 and 1, as those are the class and function
    for( $i = 2; $i < $paramCount; $i++ ) {
      $params[] = $getRequest[$i];
    }
    //Check if method exists
    if( method_exists($class, $getFunction) ) {
      //Always echo the function for returns. This is made for AJAX calls.
      echo call_user_func_array(array(
              $class,
              $getFunction
      ), $params);
    } else {
      die( "function $getFunction was not found in $page" );
    }
    exit();
  } else {
    //No function being called, this has to be a pageload;
    //Don't echo the function because of the templates
    $class->renderTemplate();
  }

-- 以上是任何 MVC 的基本形式,它是我一直使用的简化版本。基本上,这为您的网站提供了一个单一的入口点(index.php)并禁止其他所有内容。这样,没有人可以做任何你的路由不允许的事情。通过拥有稳定的class-&gt;function 路由系统,没有人可以疯狂地浏览您的网址,寻找漏洞。

从现在开始,您的网址将如下所示: http://website.com/page2 将是 page2 类,并在该类中做任何你想做的事情。

http://website.com/page2/functionOfPage2Class 可用于 ajax 调用。

旁注: 这样,您也可以在模板中调用上述函数类,只需使用$this-&gt;Myfunction();

现在,我不会说这是您将获得的最佳方式或最佳答案。我要说的唯一一件事是,这与我迄今为止所使用的大致相同,据我所知,我的客户都没有被黑客入侵。

如果我做错了什么或有任何问题,请告诉我。

你实际上并不需要所有这些类等等

我认为重要的是要提及您实际上不必使用我给您的这个结构。这只是一个例子。显然,你可以做任何你想做的事情,只要你通过index.php 路由你想做的任何事情。这就是示例脚本的全部意义所在。

长话短说:您将所有 php 文件放入一个子文件夹并拒绝对其进行所有访问。

【讨论】:

  • 好的,谢谢!实际上我不是php开发人员)只是想问你们是怎么做的。
猜你喜欢
  • 1970-01-01
  • 2010-10-12
  • 2017-10-27
  • 2021-08-10
  • 1970-01-01
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
相关资源
最近更新 更多