我已经进行了正确的数据库配置,view_login,并且我已经将一些数据插入到 ms_user 表中。现在,我如何使用用户名、密码、角色('学生''老师''校长')等角色创建登录名。
我发现这是为了指导我,但该网站仅提供 2 信息登录、用户名和密码。我在哪里添加第三个“角色”信息?
这是 view_login.php
<html>
<head> <title>Selamat datang pada QB Project </title></head>
<script>
function loginsukses()
{
alert('Login Success');
}
</script>
<body>
<center><h1>Selamat datang pada QB Architecture</h1></center>
<div id="form">
<form method="POST" action="">
<table border="1" bordercolor="gold" style="background-color:#FFFFFF" width="20%" cellpadding="2" cellspacing="4">
<tr>
<td class="td">Username</td>
<td>
<input type="text" id ="username" name ="username" class="gui" " />
</td>
</tr>
<tr>
<td class="td">Password</td>
<td class="td"><input type="password" id="password" name="password" class="gui" /></td>
</tr>
<tr>
<td class="td">Jabatan</td>
<td class="td"><select name="jabatan" id="Jabatan" class="gui">
<option value=“Accounting”>Accounting</option>
<option value=“Direktur”>Direktur</option>
<option value=”HeadDivisi”>Head Divisi</option>
</select></td>
</tr>
<tr>
<td></td>
<td class="td">
<input type="submit" name="submit" value="Login" class="button" onclick="loginsukses()" />
</td>
</tr>
</form>
</div>
</body>
</html>
这是模型文件夹中的登录功能
<?php
Class Login extends CI_Model
{
function login($username, $password)
{
$this -> db -> select('username, password, jabatan');
$this -> db -> from('ms_user');
$this -> db -> where('username = ' . "'" . $username . "'");
$this -> db -> where('password = ' . "'" . MD5($password) . "'");
$this -> db -> where('jabatan = ' . "'" . $jabatan . "'");
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
?>
这是控制器文件夹中的 validate_login 函数
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ValidateLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Login','',TRUE);
}
function index()
{
//This method will have the credentials validation
$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');
$this->form_validation->set_rules('Jabatan', 'Username', rim|required|xss_clean');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('qb_main', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password, $jabatan);
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;
}
}
}
?>
我对这个框架很陌生,所以我不明白我上面写的内容,但我愿意学习,请帮助我