我正在使用CodeIgniter开发新项目。目前我卡在登录模块中。我的代码和数据库架构如下所示:
这是我的数据库架构(数据库:成员资格,表:用户):
---------------------------------------------------------------------------------------------------------------------------------
id | nama_depan | nama_belakang | username | password | email_address | jabatan |
-----|--------------|---------------|------------|----------------------------------|-----------------------|-------------------|
2 | Joe | Sambera | sambera | e10adc3949ba59abbe56e057f20f883e | sambera@gmail.com | Manajemen |
10 | holmes | Richard | holmes | 81dc9bdb52d04dc20036dbd8313ed055 | holmes@gmail.com | Event Organizer |
---------------------------------------------------------------------------------------------------------------------------------
这是我的模型(membership_model):
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('jabatan', $this->input->post('jabatan'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'nama_depan' => $this->input->post('first_name'),
'nama_belakang' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'jabatan' => $this->input->post('jabatan'),
'password' => md5($this->input->post('password'))
//'jabatan' => $this->input->post('jabatan'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
这是我的观点(登录):
<?php $this->load->view('includes/header'); ?>
<h1>Login</h1>
<?php
echo form_open('login/validate_credentials');
echo form_input('username', 'Username');
echo form_password('password', 'Password');
echo form_submit('submit', 'Login');
//echo anchor('login/signup', 'Create Account');
echo form_close();
?>
<?php $this->load->view('includes/footer'); ?>
这是我的控制器(登录):
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
//redirect('site/members_area');
if($jabatan == "Manajemen"){
redirect('site/manajemen_page');
}
else{
redirect('site/eo_page');
}
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
//$data['main_content'] = 'signup_form';
//$this->load->view('includes/template', $data);
$this->session->set_userdata($data);
redirect('site/signup_page');
}
}
这是另一个Controller(网站):
<?php
class Site extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function members_area()
{
$this->load->view('logged_in_area');
}
function signup_page() // signup form
{
$this->load->view('signup_form');
}
function manajemen_page() // just for Manajemen
{
$this->load->view('manajemen_page');
}
function eo_page() // just for EO
{
$this->load->view('eo_page');
}
function another_page() // just for sample
{
echo 'good. you\'re logged in.';
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo '<br><br><br><br><br><br><br>';
echo '<strong>';
echo '<center>You don\'t have permission to access this page. <a href="../login">Login</a></center>';
echo '</strong>';
die();
//$this->load->view('login_form');
}
}
}
正如您在Controller / Login中看到的,在下面的代码中,我想尝试验证登录用户。如果登录站点的Managament将重定向到控制器/站点中的manajemen_page。如果登录的事件管理器,该站点将被重定向到控制器/站点中的EO_page。 (我的代码无效)
if($jabatan == "Manajemen"){
redirect('site/manajemen_page');
}
else{
redirect('site/eo_page');
}
你们能帮助我吗?
答案 0 :(得分:1)
好的,你在这里错过了几件事。首先,您没有从模型中返回结果,因此您的变量$jabatan
未定义。其次,你的模型中不应该有input->post()
,postdata只能在你的控制器中处理。
您的控制器功能validate_credentials
应该像这样重写:
function validate_credentials() {
$this->load->model('membership_model');
$postdata = $this->input->post();
//you can run any checks or cleaning you need to do to the postdata your passed in, then send it to model
$result = $this->membership_model->validate($postdata);
if ($result) { // if the user's credentials validated...
$data = array(
'username' => $postdata['username'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
//redirect('site/members_area');
if ($result->jabatan === "Manajemen") { //CHANGE
redirect('site/manajemen_page');
} else {
redirect('site/eo_page');
}
} else { // incorrect username or password
$this->index();
}
}
和您的模型函数validate
:
function validate($data) {
$this->db->select("*");
$this->db->from("user");
$this->db->where('username', $data['username']);
$this->db->where('password', md5($data['password']));
$query = $this->db->get();
if ($query->num_rows == 1) {
return $query->row();
}else{
return false;
}
}