如何在PHP中限制用户更改URL
我想知道,如果控制器中有一个方法需要参数,并且用户手动更改URL中的参数,然后按enter键,那么它应该显示默认页面。下面是我的引导,然后我已经为URL错误创建了一个错误控制器。所以,请给出一些编码指南,或者如果我的代码中有什么东西,请更改它。提前谢谢如何在PHP中限制用户更改URL,php,url-routing,Php,Url Routing,我想知道,如果控制器中有一个方法需要参数,并且用户手动更改URL中的参数,然后按enter键,那么它应该显示默认页面。下面是我的引导,然后我已经为URL错误创建了一个错误控制器。所以,请给出一些编码指南,或者如果我的代码中有什么东西,请更改它。提前谢谢 <?php class App { protected $controller = 'indexController'; protected $method = 'index';
<?php
class App
{
protected $controller = 'indexController';
protected $method = 'index';
protected $params = array();
public function __construct()
{
$url = $this->parseUrl();
//print_r($url);
if (isset($url[0]))
{
if (file_exists('app/controllers/'.$url[0].'.php'))
{
$this->controller = $url[0];
unset($url[0]);
}
require_once('app/controllers/'.$this->controller.'.php');
$this->controller = new $this->controller;
}
else
{
$error = new errorController();
$error->setError("Page Not Found");
echo $error->getError();
}
if (isset($url[1]))
{
if (method_exists($this->controller,$url[1]))
{
$this->method = $url[1];
unset($url[1]);
}
else
{
$error = new errorController();
$error->setError("Page Not Found");
echo $error->getError();
}
}
else
{
$error = new errorController();
$error->setError("Page Not Found");
echo $error->getError();
}
$this->params = $url ? array_values($url) : array();
call_user_func_array(array($this->controller,$this->method),$this->params);
}
public function parseUrl()
{
if (isset($_GET['url']))
{
return $url =explode('/',filter_var(rtrim($_GET['url'],'/'),FILTER_SANITIZE_URL));
}
}
}
方法本身必须检查提供的参数是否有效,如果无效则引发异常。之后,只需捕获要显示的异常、触发器和错误页面。似乎可以使用POST和$\u POST,而不是GET。如果($result==false){//require_once('app/controllers/errorController.PHP');$error=new errorController();$error->setError(“未找到页面”),我刚刚在我的模型类中添加了这段代码$error->randerError();我尝试了你的建议,这也是有效的,这是多态性的一个很好的例子:)太好了!谢谢@Muhabutti。