当 CodeIgniter 中有额外的 URL 段时,如何显示 404


How do you show a 404 when there are extra URL segments in CodeIgniter?

基本上,我有一个CodeIgniter网站,通过这个网址注册用户:
http://website.com/user/register/1

不幸的是,当您在 URL 中传递额外的参数时,如下所示:http://website.com/user/register/1/extraargs/extraargs

这是我的代码:

<?php
class User extends CI_Controller
{
    public function register($step = NULL)
    {
        if (($step!=NULL)&&($step == 1 || $step == 2 || $step == 3)) {
            if ($step == 1){
                $this->load->view('registration_step1');
            }
        } else {
            show_404();
        }
    }
}
?>

它不显示 404。它只是忽略额外的参数。我想要它,以便额外的参数不会影响 URL。如果有额外的 URL 参数,我想显示 404。你是怎么做到的?此外,额外的 URL 段会影响安全性吗?谢谢。

不确定你为什么要这样做,但你可以使用func_num_args()并从中验证,即

public function register($step = NULL)
{
  if ( func_num_args() > 1 ) show_404();
}