0

要创建登录系统,我一直在使用本教程 - www.codefactorycr.com/login-with-codeigniter-php.html。问题是当我将数据库中的用户名和密码输入表单并单击登录时,它会出现 404 错误页面并且找不到 verifylogin.php。我已经在下面包含了我的所有文件,但我仍然无法弄清楚为什么这没有登录到 home.php

自动加载.php

        Prototype:
        $autoload['libraries'] = array('database', 'session', 'xmlrpc');


        $autoload['libraries'] = array();

路由.php

          $route['login'] = "login";
          $route['news/update/(:num)'] = 'news/update/$1';
          $route['news/create'] = 'news/create';
          $route['news/(:any)'] = 'news/view/$1';
          $route['news'] = 'news';
          $route['(:any)'] = 'pages/view/$1';
          $route['default_controller'] = 'pages/view';

配置文件

          $config['base_url']   = 'http://shaek.co.uk/codeigniter';
          $config['index_page'] = 'index.php';

控制器 - login.php

               <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

               class Login extends CI_Controller {

                function __construct()
                {
                    parent::__construct();
                }

                 function index()
                 {
                    $this->load->helper(array('form'));
                    $this->load->view('login_view');
                 }

              }

           ?>

控制器 - verifylogin.php

         <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

      class VerifyLogin extends CI_Controller {

           function __construct()
           {
        parent::__construct();
        $this->load->model('user','',TRUE);
              }

          function index()
                   {
         //This method will have the credentials validation
             $this->load->library('form_validation');

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

         if($this->form_validation->run() == FALSE)
              {
       //Field validation failed.  User redirected to login page
          $this->load->view('login_view');
          }
             else
             {
              //Go to private area
             redirect('home_view', 'refresh');
            }

            }

            function check_database($password)
            {
                //Field validation succeeded.  Validate against database
                  $username = $this->input->post('username');

               //query the database
                    $result = $this->user->login($username, $password);

                if($result)
         {
                 $sess_array = array();
                foreach($result as $row)
       {
               $sess_array = array(
               'id' => $row->id,
                username' => $row->username
     );
              $this->session->set_userdata('logged_in', $sess_array);
            }
              return TRUE;
           }
              else
           {
                $this->form_validation->set_message('check_database', 'Invalid username or password');
             return false;
           }
          }
         }
       ?>

控制器 home.php

            <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
       session_start(); //we need to call PHP's session object to access it through CI
       class Home extends CI_Controller {

              function __construct()
            {
              parent::__construct();
            }

              function index()
            {
              if($this->session->userdata('logged_in'))
            {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->view('home_view', $data);
           }
               else
              {
                 //If no session, redirect to login page
                   redirect('login', 'refresh');
               }
             }

                 function logout()
             {
                $this->session->unset_userdata('logged_in');
                session_destroy();
                redirect('home', 'refresh');
              }

           }

        ?>

模型 - users.php

              <?php
                 Class User extends CI_Model
              {
                function login($username, $password)
              {
                 $this -> db -> select('id, username, password');
                 $this -> db -> from('users1');
                 $this -> db -> where('username', $username);
                 $this -> db -> where('password', MD5($password));
                 $this -> db -> limit(1);

                 $query = $this -> db -> get();

                 if($query -> num_rows() == 1)
           {
                    return $query->result();
                  }
             else
              {
                    return false;
            }
        }
     }
   ?>

视图 - home_view.php

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title>Simple Login with CodeIgniter - Private Area</title>
      </head>
        <body>
          <h1>Home</h1>
               <h2>Welcome <?php echo $username; ?>!</h2>
                <a href="home/logout">Logout</a>
            </body>
             </html>

意见 - login_view.php

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Simple Login with CodeIgniter</title>
   </head>
   <body>
      <h1>Simple Login with CodeIgniter</h1>
     <?php echo validation_errors(); ?>
      <?php echo form_open('verifylogin'); ?>
      <label for="username">Username:</label>
       <input type="text" size="20" id="username" name="username"/>
       <br/>
     <label for="password">Password:</label>
     <input type="password" size="20" id="passowrd" name="password"/>
      <br/>
       <input type="submit" value="Login"/>
            </form>
         </body>
         </html>
4

3 回答 3

0

在相关说明中,您还必须将模型类名称更改为“用户”而不是“用户”。


正如 Jaspal 所说,没有必要:

您有控制器/类名称问题。

如果您将控制器命名为“verifylogin.php”,则必须将类命名为相同的名称,且首字母大写。您将班级命名为“VerifyLogin”。它必须是“验证登录”。

于 2013-03-13T15:08:56.927 回答
0

首先,

$route['(:any)'] = 'pages/view/$1'; 
//((:any) will match a segment containing any character.) 
//and throw it to pages/view/$1

我建议将其更改为

$route['pages/view/(:any)'] = 'pages/view/$1'; 

http://shaek.co.uk/codeigniter/index.php/verifylogin本身就是一个 404 页面

此外,http ://shaek.co.uk/codeigniter/index.php/pages/view/无法访问。内部路由导致请求被传递到页面控制器视图操作。

于 2013-03-13T15:06:31.713 回答
0

将“ Controller - verifylogin.php”类名从VerifyLogin 更改为Verifylogin,看看它是否有效。

似乎无法找到控制器名称,可能是由于类名。

你的另一件事在verifylogin.php用户名之前错过了一个“'”。更改自:

 $sess_array = array(
               'id' => $row->id,
                username' => $row->username

);

至:

 $sess_array = array(
               'id' => $row->id,
                'username' => $row->username

);
于 2013-03-13T16:49:31.777 回答