我正试图了解PHP
并使用CodeIgniter
框架。我最近一直在关注本教程:
http://www.codefactorycr.com/login-with-codeigniter-php.html
我非常有条理地完成了它,并且小心翼翼地确保我在编写代码时没有犯错误。但是,当我在浏览器中运行项目时,在尝试提交登录详细信息后,我收到此错误。
未找到 在此服务器上找不到请求的URL / LoginTut / login / VerifyLogin。
我很久以前遇到了类似的问题,当时我正在关注另一个教程而无法弄明白,所以放弃了这个项目。
这就是我配置CI的方式
routes.php文件
$route['default_controller'] = "login";
的config.php
$config['base_url'] = 'http://localhost/LoginTut/';
我把这个留空了。
$config['index_page'] = '';
我也确保我正在加载所有相关的库和助手。
autoload.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url');
以下是我的所有PHP文件..
user.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>
的login.php
class Login extends CI_Controller{
function _construct(){
parent::_construct();
}
function index(){
$this->load->helper(array('form'));
$this->load->view('login_view');
}
}
verify_login.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
文件的控制器结束。
这是登录表单的帖子操作
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>
非常感谢。
答案 0 :(得分:2)
我也回答了你的另一篇文章,但重申一下:
您将控制器命名为“verify_login.php”,但您的类名为“VerifyLogin”。在Codeigniter中,您的类需要与您的文件命名相同,只需使用大写字母。为了使该页面正常工作,您应该将控制器文件重命名为“verifyLogin.php”或将您的类重命名为“Verify_Login”。
答案 1 :(得分:1)
关闭重写一段时间。首先使用正常的CodeIgniter URL修复此问题:
CodeIgniter网址的工作方式如下:
<base_url>/<index-page>/<controller>/<methodOfController>/<parameters>
当您请求.../login/VerifyLogin
无效时,因为登录和 VerifyLogin 都是控制器。 CodeIgniter将“认为”您要在控制器类 login 中调用方法 VerifyLogin()。您应该拨打.../VerifyLogin/index
。
您将$config['index_page']
留空了。在CodeIgniter中,您无法直接调用控制器上的方法;你有没有让CodeIgniter为你做。这意味着CodeIgniter必须先运行,然后解析URL并将请求委托给正确的控制器和方法。这就是config * index_page *的用途 - 启动CodeIgniter。您必须通过此文件运行所有请求。
CodeIgniter中包含 index.php (它以行require_once BASEPATH.'core/CodeIgniter.php';
结尾)。将此作为index_page使用。
完成此操作后,您的完整网址就是:
http://localhost/LoginTut/index.php/VerifyLogin/index/<parameters>