php codeigniter 中的登录表单代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36931330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
code for login form in codeigniter
提问by php
I'm still learning my way around in CodeIgniter.
我仍在学习如何使用 CodeIgniter。
I'd like to create a login form in my codeigniter application. My view section is below:
我想在我的 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>
Controller section:
控制器部分:
<?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');
}
}
}
}
?>
Error Message:
错误信息:
The requested URL /afi/Login/process was not found on this server.
在此服务器上找不到请求的 URL /afi/Login/process。
How can I resolve this error?
我该如何解决这个错误?
Model section:
型号部分:
<?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;
}
}
?>
回答by Chitranjan kr. ranjan
Replace $query->num_rows
with $query->num_rows()
in your model and then try again.
在您的模型中替换$query->num_rows
为$query->num_rows()
,然后重试。
回答by Ridle Sambow
try use site_url(). change :
尝试使用 site_url()。改变 :
<form action="<?php echo base_url();?>Login/process" method='post' name='process' enctype="multipart/form-data">
<form action="<?php echo base_url();?>Login/process" method='post' name='process' enctype="multipart/form-data">
to :
到 :
<form action="<?php echo site_url('Login/process');?>" method='post' name='process' enctype="multipart/form-data">
<form action="<?php echo site_url('Login/process');?>" method='post' name='process' enctype="multipart/form-data">
base_url
produce :
base_url
生产 :
while site_url
produce :
而site_url
产生:
回答by Neeraj Khatediya
try this
<?php echo form_open('Login/process')?>
its best way to resolve your problem
试试这
<?php echo form_open('Login/process')?>
是解决问题的最佳方法