-1
我仍然在CodeIgniter中學習我的方法。Codeigniter中的登錄表單代碼
我想在我的codeigniter應用程序中創建一個登錄表單。 我的看法部分是如下:
<html>
<head>
<title>Jotorres Login Screen | Welcome </title>
</head>
<body>
<div id='login_form'>
<form action="<?php echo base_url();?>Login/process" method='post' name='process' enctype="multipart/form-data">
<h2>User Login</h2>
<br />
<?php if(! is_null($msg)) echo $msg;?>
<label for='username'>Username</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
控制器部分:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login controller class
*/
{
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
$this->load->view('login_view', $data);
}
public function process(){
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->index($msg);
}else{
// If user did validate,
// Send them to members area
redirect('Home');
}
}
}
}
?>
錯誤消息:
所請求的網址/ AFI /登錄/過程未在此服務器上找到。
如何解決此錯誤?
Model部分:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login model class
*/
class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
// Prep the query
$this->db->where('username', $username);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('users');
// Let's check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'userid' => $row->userid,
'fname' => $row->fname,
'lname' => $row->lname,
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
return false;
}
}
?>
請妥善張貼您的問題和代碼。 – adi
在你的控制器的部分有一個額外的'{}'標記(也許你剛貼錯了)。可能的問題,你的.htaccess文件錯誤,或者你沒有聲明你的base_url(在config.php中),或者你的codeigniter的配置文件中有其他內容。你的代碼(豪爾赫託雷斯的代碼)似乎是好的(除了惱人的'在這裏輸入代碼'行)。 – skyyler
我已經聲明base_url ...仍然是相同的錯誤 – php