我正在尝试了解PHP
并使用CodeIgniter
框架。我最近一直在关注这个教程:
http://www.codefactorycr.com/login-with-codeigniter-php.html
我非常有条不紊地完成了它,并小心地确保我在编写代码时没有出错。但是,当我在浏览器中运行我的项目时,我在尝试提交登录详细信息后收到此错误。
未找到 在此服务器上未找到请求的 URL /LoginTut/login/VerifyLogin。
不久前,当我在学习另一个教程时遇到了类似的问题,无法弄清楚所以放弃了这个项目。
这就是我配置 CI 的方式
路由.php
$route['default_controller'] = "login";
配置文件
$config['base_url'] = 'http://localhost/LoginTut/';
我把这个留空了。
$config['index_page'] = '';
我还确保我正在加载所有相关的库和助手。
自动加载.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url');
下面是我所有的PHP文件..
用户.php
class User extends CI_Model{
function login($username, $password){
$this -> db -> select('id, username, password');
$this -> db -> from ('users');
$this -> db -> where ('username', $username);
$this -> db -> where ('password', md5($password));
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1){
retrun $query->result();
}
else{
return false;
}
}
}
login_view.php
<?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>
登录.php
class Login extends CI_Controller{
function _construct(){
parent::_construct();
}
function index(){
$this->load->helper(array('form'));
$this->load->view('login_view');
}
}
验证登录.php
class VerifyLogin extends CI_Controller{
function _construct(){
parent::_construct();
$this->load->model('user', '', TRUE);
}
function index(){
$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){
//Validation failed, user redirected to login page...
$this->load->view('login_view');
}
else{
//Login success. Go to members only area
redirect('home', 'refresh');
}
}
function check_database($password){
//Field validation succeeded. Validate against db
$username = $this->input->post('username');
//query db
$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
加载home_view.php
文件的控制器。
这是登录表单的 Post 操作
http://localhost/LoginTut/login/VerifyLogin
我非常感谢与此问题相关的任何帮助,我已经为此苦苦挣扎了好几天,而且它开始让我很恼火。我有一种感觉这是我的代码中出现的非常小的问题。我只是没有足够的知识来弄清楚。
注意:我在论坛上读到它可能与 .htaccess 问题有关,但我不知道那是什么!
.htaccess 文件
<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
非常感谢。