Php 404页面未找到CodeIgniter登录

Php 404页面未找到CodeIgniter登录,php,forms,codeigniter,login,http-status-code-404,Php,Forms,Codeigniter,Login,Http Status Code 404,这是我第一次使用CodeIgniter,我正在尝试创建一个登录表单。每当我点击我创建的submit按钮时,就会出现404页面Not Found错误 我在view文件夹中创建了一个view_login.php,代码如下: <?php echo form_open('user/login') ?> <ul> <li> <label> username </label> <div> <?php echo form_input

这是我第一次使用CodeIgniter,我正在尝试创建一个登录表单。每当我点击我创建的submit按钮时,就会出现404页面Not Found错误

我在view文件夹中创建了一个view_login.php,代码如下:

<?php echo form_open('user/login') ?>
<ul>
<li>
<label> username </label>
<div>
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?>
</div>
</li>
<li>
<label> password </label>
<div>
<?php echo form_password(array('id' => 'password', 'name' => 'password')); ?>
</div>
</li>
<li><?php echo validation_errors(); ?></li>
<li>
<?php echo form_submit(array('name' => 'submit'), 'login'); ?>
</li>
任何帮助都将不胜感激:)

应该是

function user()
{
    parent::__construct();
}
你也正在加载

$this->load->view('pages/view_login'); 
and
$this->load->view('view_login');
检查代码,确保这些视图同时存在? 此外,我真的认为您可以删除所有这部分:

function index()
{
    $this->login();
}

另外,您正在表单open中使用index.php,您是否正在使用htacces从url中删除它?404找不到页面

我知道你最终会删除你的问题,就像你之前三个问题一样(我知道),但是嘿

你做了很多错事。让我们做一些清理:

<?php  
class User extends CI_controller
{

// constructor is useless here, let's remove it.   

function index()
{
    redirect('login','refresh');
    // this is useless too, but just in case someone navigates to the url 'user'
}

function login()
{
    // you are calling form_validation methods, and loading the library _after_. 
    // It's the other way around.
    // Also, no need to load the form helper, it's already loaded
    // by the form_validation library

    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'required|trim|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|trim|max_length[200]|xss_clean');

     $this->load->view('pages/view_login');

     if ($this->form_validation->run() == FALSE) {
         $this->load->view('view_login');
     {
     else {
        //extract($_POST);
        // this is quite bad practice imho (it's like goring back to register_globals).
        // You should fetch the post content directly:
         $user_id = $this->user_model->check_login($this->input->post('username',
                    $this->input->post('password');
      }    
  }
?>

这就是您的问题所在。我怀疑您将控制器名称与视图中的子目录混淆:

$this->load->view('user/view_login'); 
目前,您正在从名为user的文件夹/目录加载view_login.php。如果页面不在引用目录中,则会出现404页面未找到错误。它不存在

我认为这是一个错误,因为用户实际上是您的控制器,只能从以下表单中使用:

<?php echo form_open('user/login'); ?>
尝试将view_login.php直接放入view文件夹,并使用此文件夹调用它:

view/
    view_login.php

$this->load->view('view_login');
同理

$this->load->view('templates/header', $data);   
$this->load->view('user/view_login');
$this->load->view('templates/footer');
还意味着header.php和footer.php位于子目录“模板”中。任何一个错误都会导致404错误。您可能希望通过回显一些字符串来对负载视图和测试进行注释——只是为了证明您的代码都很好

通过测试其他url是否正常工作,验证您是否可能受到htaccess文件的干扰

[更新] 如果您怀疑表单操作是否正确,请尝试使用
base\u url()
,如果配置正确

<?php echo form_open(base_url().'user/login') ?>


您正在从url中删除index.php吗?@donotusetabtodigitthisnick no该url指向index.php/user/login,但出现404错误,您知道还有其他url吗?工作检查主web根目录中是否有htaccess文件检查firebug中表单指向的路径,并尝试直接访问它。并在您的登录函数中写入echo“test”;模具();看看它是否是缩写的。你的根目录中有一个.htaccess文件吗?所有其他URL都工作正常(回答你上面的评论)谢谢,我将此更改为构造,但仍然没有更改,我还删除了最后一个$this->load,但什么也没有发生:(这太烦人了。一定与我的登录功能有关?@benshapiro你确定你有视图/pages/view_login.php吗?还可以在应用程序/config/config.php内部搜索$config['index_page']=“index.php”(必须如我所示),您是否更改了任何其他配置文件?当然,我确定,是的,我的配置索引设置为index.php只是修复了您提到的所有内容,不要担心过于苛刻,我可以做一些建设性的批评..我拿出index.php部分,它带来了一个不同的404 not found错误。我需要更改我的基本url吗?+1 for正确的form\u open。不要引用完整的URL,只需form\u open(controller*/*方法)我已经更改了我的form\u open并清理了我的代码,但没有更改更新的damien,我已经在($this语句)中添加了一些代码看看它是否有帮助,但它没有谢谢,我不确定这是否有帮助,但我的view\u login.php位于views\pages\view\u login下。虽然用户控制器应该能够调用此页面,但这就是我通过user\viewlogin调用它的原因。我去掉了根,尽管像你上面说的那样,仍然没有更改,这可能与我的user\u mod有关吗php?我的view\u登录确实有效,因为页面与我创建的表单一起完美加载,除非我提交表单时,当您调用错误的视图时,没有任何响应。如果您的视图位于子目录页面中,那么您应该在代码中使用页面,而不是用户。我已经在我的答案中添加了一个更新。其次,您有一些如果提交不起作用,您的代码可能会出错。如果您在
视图\pages\view\u login
中有视图,请查看编辑以查看对代码的修改。此外,我找不到您使用表单\u validation->rule()的位置;谢谢,其他每个URL都可以正常工作,但它们不是:'CodeIgniter/index.php/user/login'或'CodeIgniter/index.php/user/register'。我已将我的view\u login和view\u register移动到views目录中,这样它们就不会隐藏在任何其他文件夹下。我需要一个公共函数来查看吗?你看到我上次的更新了吗?你是如何配置你的codeignit的er配置?我不明白你说的codeigniter/index.php/user/login是什么意思。codeigniter在那里做什么?应该是
http://localhost/yoursite/index.php/user/login
http://localhost/yoursite/user/login
?要删除index.php,请阅读:或
<?php echo form_open('user/login'); ?>
view/
    user/
        view_login.php
view/
    view_login.php

$this->load->view('view_login');
$this->load->view('templates/header', $data);   
$this->load->view('user/view_login');
$this->load->view('templates/footer');
<?php echo form_open(base_url().'user/login') ?>
        public function login() {
        $this->load->library('form_validation');

        $data['title'] = 'Log In';
        if ($this->input->post()) {
            print_r(); //test if you have values

            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

            //You could set delimiter

            if ($this->form_validation->run() == FALSE) {
                //What so ever you which to do if validation fails. { else {
                        $username = $this->input->post('username'); //Make your code a bit readable
                        $password = $this->input->post('password'); //Make your code a bit readable

                        $user_id = $this->User_model->check_login($username, $password);
                        $data["user_id"] = $user_id; //you can access $user_id from your view
                    }
                }
                //Pass $data to views, you might need it in more than one view. Also change directory from user to pages to match what you stated in comment below. And please make sure these views are in the directories specified below...

                $this->load->view('templates/header', $data);
                $this->load->view('pages/view_login', $data);
                $this->load->view('templates/footer', $data);
            }