【问题标题】:CodeIgniter, Requested URL not found on serverCodeIgniter,在服务器上找不到请求的 URL
【发布时间】:2020-03-29 18:00:14
【问题描述】:

我目前正在 CodeIgniter 中开发一个系统,该系统需要通过登录和注册作为基础进行用户身份验证,以便使用系统的其余部分。每当我尝试调用 Controller 函数时,我的系统上都会出现 404 Not Found 问题,这在表单提交中尤为普遍。我已经从下面的控制器、模型、视图、路由和 Htaccess 中发布了我的相关代码。我不完全确定导致 404 not found 错误的原因,任何帮助将不胜感激。

控制器:

// handles register page
public function register() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = "Register";

    $this->form_validation->set_rules('fullname','Full Name','required');
    $this->form_validation->set_rules('accountid','Account ID','required');
    $this->form_validation->set_rules('password','Password','required');
    $this->form_validation->set_rules('type','Account Type','required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('register', $data);
    } else {
        $fullname = $this->input->post('fullname');
        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));
        $accounttype = $this->input->post('type');

        $this->system->registeruser($username, $password, $fullname, $accounttype);
        echo "User Registration Complete";
    }
}

型号:

public function registeruser($accountid, $password, $fullname, $accounttype) {
    $query = "INSERT INTO users VALUES($fullname','$accountid','$password','$accounttype')";
    $this->db->query($query);
}

查看:

<?php
    echo form_open('main/register');
    echo validation_errors(); ?>
        <div id="name-input">
            <?php
                $data = array(
                    'name' => 'fullname',
                    'value' => $this->input->post('fullname'),
                    'placeholder' => 'Full Name',
                    'class' => '',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_input($data);
            echo "</p>"; ?>
        </div>

        <div id="username-input">
            <?php
                $data = array(
                    'name' => 'accountid',
                    'value' => $this->input->post('accountid'),
                    'placeholder' => 'User ID',
                    'class' => 'username',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_input($data);
            echo "</p>"; ?>
        </div>

        <div id="password-input">
            <?php
                $data = array(
                    'name' => 'password',
                    'value' => $this->input->post('password'),
                    'placeholder' => 'Password',
                    'class' => 'passwordd',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_password($data);
            echo "</p>"; ?>
        </div>

        <div id="account-type">
            <?php
                $options = array(
                    'student' => 'Student',
                    'lecturer' => 'Lecturer' );

                $data = array(
                    'name' => 'type',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_dropdown($data, $options);
            echo "</p>"; ?>
        </div>

        <div id="submit-button">
            <?php
                $data = array(
                    'name' => 'register',
                    'value' => 'Register Account',
                    'class' => 'register',
                    'style' => 'width:100%; padding:0.5em;' );

                echo form_submit($data); ?>
        </div>
<?php echo form_close(); ?>

路线:

$route['main/register'] = 'main/register';
$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

上面的所有代码都与我的注册页面/它的相关功能有关,我的系统上目前有多个这些错误,但它们似乎都是同一件事,我想解决一个也可以解决其余的问题。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我不知道您的问题的确切原因,但以下解决方案应该可以工作。

    删除

    echo form_open('main/register');
    

    行和添加

    echo "<form action='' method='post' >";
    

    (将action属性留空,会向当前页面提交数据)

    删除

    $route['main/register'] = 'main/register';
    

    行。

    额外建议:不要使用md5(),因为它不安全。请改用password_hash()

    【讨论】:

      【解决方案2】:

      尝试删除路由$route['main/register'] = 'main/register';。此外,将方法 POST 提供给表单。并且输入字段名称在控制器和模型中不匹配。


      编辑:

      表单操作应该是一个完整的 URL-
      echo form_open(base_url()."/controllername/mehod"); 在您的情况下应该是 echo form_open(base_url()."/main/register");

      如果你还没有设置base_url 那么

      第一步:打开application\config文件夹下的config.php文件

      第 2 步:将 $config['base_url'] 的值设置为您的网站路径

      $config['base_url'] = "http://localhost/foldername/index.php";
      

      这应该会有所帮助。 :)

      【讨论】:

      • 所以我从 routes.php 中删除了路由,修复了输入字段名称,但仍然出现错误,如何将 method="post" 添加到 echo form_open() ?
      • $attribute['method'] = 'POST'; echo form_open(THE_ACTION_URL, $attribute);
      • 我尝试添加,它仍然不起作用,仍然找不到 404。
      猜你喜欢
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 2017-08-07
      • 2018-04-19
      • 2017-03-21
      • 2010-10-16
      • 1970-01-01
      相关资源
      最近更新 更多