【发布时间】:2012-12-19 22:13:46
【问题描述】:
长期以来,我一直是stackoverflow的狂热读者,现在轮到我提问了。
我遵循http://net.tutsplus.com/tutorials/php/easy-authentication-with-codeigniter/ 教程并设法让它完美地为我自己工作,但是我需要稍微扩展系统。在验证用户后,我需要能够提取经过身份验证的人员的其他行字段并存储在会话变量中,以便在其他控制器中用于将信息发送回数据库。
例如,我想在会话变量 $f_name 中有一个,我需要他们的 $username。 我从高处到低处都找到了答案,但它们只是让我感到困惑。
我的模特:
class admin_model extends CI_Model {
function __construct( )
{
}
public function verify_user($user_id, $password)
{
$q = $this->db->where('user_id', $user_id)
->where('password', sha1($password))->limit(1)->get('users');
if ( $q->num_rows > 0 ){
return $q->row();
}
return false;
}
}
我的控制器:
class admin extends CI_Controller {
function __construct()
{
parent:: __construct();
session_start();
}
public function index()
{
if ( isset($_SESSION['user_id'])){
redirect('welcome');
}
$this->load->library('form_validation');
$this->form_validation->set_rules('user_id', 'User ID', 'required|min_length[8]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
if ($this->form_validation->run() !==false){
$this->load->model('admin_model');
$res = $this
->admin_model
->verify_user(
$this->input->post('user_id'),
$this->input->post('password')
);
if ($res !== false) {
$_SESSION['user_id'] = $this->input->post('user_id');
redirect ('welcome');
}
}
$this->load->view('login_view');
}
public function logout(){
session_destroy();
redirect ('admin');
}
}
再次感谢大家,期待您的回答\建议
【问题讨论】:
-
为什么不使用 CI 的数据库会话?这样您就可以在该表中查询相关数据。
-
我是否有可能获得一个示例,说明如何使用 CI 的数据库会话实现我需要的功能?我还是 CI 新手
-
你不需要任何东西,只需设置一个配置参数。当您获取会话元素时,所有查询都会自动处理 ($this->session->userdata('element'))
-
我不太明白,能否请您提供更多详细信息?
标签: php codeigniter session authentication