我試圖讓我的腦袋圍繞PHP
並使用CodeIgniter
框架。我最近一直在按照本教程:使用CodeIgniter的簡單登錄系統在登錄時返回404
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['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_view.php
文件的home.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>
非常感謝。
你有'.htaccess'文件嗎?如果沒有,請在您的CI安裝的根目錄中添加一個,如下所示:http://stackoverflow.com/questions/14827335/cannot-find-mod-rewrite-for-codeigniter/14828424#14828424 – Mudshark 2013-02-18 15:55:54
@Mudshark Checked我的'.htacces'文件。它位於根文件夾中,它的內容是「全部拒絕」。我想這不太理想?我應該將代碼粘貼到您提供給此文件的鏈接中嗎? – Javacadabra 2013-02-18 16:00:18
試試看:-) – Mudshark 2013-02-18 16:03:17