douchi5822 2014-08-04 22:43
浏览 61

如何限制用户在PHP中更改URL

I would like to know that if there is a method in the controller which require arguments and the user changes the argument in the URL by hand, and presses enter it should display the default page. Below is my bootstrap, then I have already created a error controller for URL error. So please give some coding guide or if there is some thing in my code, change it. Thanks in advance.

<?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));
            }
        }
    }
  • 写回答

2条回答 默认 最新

  • dtvpe4837413 2014-08-04 22:47
    关注

    The method itself must check if the parameters provided are valid and throw an exception if not. Afterwards just catch the exception and trigger and error page to be displayed.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备