我想获取欢迎管理站点(crud)的会话 fname,对于会话用户名,我已经有了它,因为它已经 delclare $_SESSION['username'] = $this->input->post('username');
,我的问题是如何从数据库中创建会话 fname?
数据库 SQL
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(32) NOT NULL,
`fname` varchar(30) DEFAULT NULL,
`lname` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)
控制器
<?php if(!defined('BASEPATH')) exit('Tidak ada akses!');
class C_login extends CI_Controller{
function __construct(){
session_start();
parent::__construct();
$this->load->model('m_user');
}
public function index(){
if(isset($_SESSION['username'])){
redirect('crud');
}
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == TRUE){
$this->load->model('m_user');
$result = $this->m_user->cek_login(
$this->input->post('username'),
$this->input->post('password')
);
if($result == TRUE){
$_SESSION['username'] = $this->input->post('username');
redirect('crud');
}
}
$this->load->view('login/v_form');
}
public function daftar(){
$data = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password)')),
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname')
);
$this->m_user->tambah_user($data);
$this->load->view('login/v_daftar',$data);
}
public function logout(){
session_destroy();
$this->index();
}
}
模型
<?php if(!defined('BASEPATH')) exit('Tidak ada access!');
class M_user extends CI_Model{
function __construct(){
parent::__construct();
}
function cek_login($username,$password){
$query = $this->db->where('username',$username)
->where('password',md5($password))
->limit(1)
->get('users');
if ($query->num_rows() > 0){
return $query->row_array();
}
else{
return FALSE;
}
}
function tambah_user($data){
return $this->db->insert('users', $data);
}
}