我个人会说这样的安全问题是没有用的。在我建立的众多网站中。这些我都没用过。
据我所知,防止人们进入您不希望他们去的地方的最佳方法是将您的所有课程集中到一个文件夹中,并拒绝从外部访问该文件夹。然后,您使用根目录中的一个文件(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->function 路由系统,没有人可以疯狂地浏览您的网址,寻找漏洞。
从现在开始,您的网址将如下所示:
http://website.com/page2 将是 page2 类,并在该类中做任何你想做的事情。
http://website.com/page2/functionOfPage2Class 可用于 ajax 调用。
旁注:
这样,您也可以在模板中调用上述函数类,只需使用$this->Myfunction();。
现在,我不会说这是您将获得的最佳方式或最佳答案。我要说的唯一一件事是,这与我迄今为止所使用的大致相同,据我所知,我的客户都没有被黑客入侵。
如果我做错了什么或有任何问题,请告诉我。
你实际上并不需要所有这些类等等
我认为重要的是要提及您实际上不必使用我给您的这个结构。这只是一个例子。显然,你可以做任何你想做的事情,只要你通过index.php 路由你想做的任何事情。这就是示例脚本的全部意义所在。
长话短说:您将所有 php 文件放入一个子文件夹并拒绝对其进行所有访问。