[英]How to set session userdata from database in codeigniter?
I have problem with my session login in codeigniter 3. I can't get data from database to insert session. 我在codeigniter 3中的会话登录有问题。我无法从数据库获取数据以插入会话。 Whats wrong with my code?
我的代码有什么问题?
My Controller 我的控制器
function __construct(){
parent::__construct();
$this->load->model('m_login');
}
function index(){
$this->load->view('v_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$cek = $this->m_login->cek_login("admin",$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
}
My Model 我的模特
<?php
class M_login extends CI_Model{
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
}
Try This
if($cek > 0){
$cek = $cek->row_array();
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
Try like this... 这样尝试...
In codeigniter row_array()
takes the first matched row in array format.So your function aksi_login()
must be like this... 在codeigniter中,
row_array()
采用数组格式的第一匹配行,因此您的function aksi_login()
必须像这样...
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$result = $this->m_login->cek_login("admin",$where);
$cek = $this->m_login->cek_login("admin",$where)->num_rows();
if($cek > 0){
$cek = $result->row_array();//no your records are in array format having matched row
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
For more refer here.. https://www.codeigniter.com/userguide3/database/results.html 有关更多信息,请参见此处。.https ://www.codeigniter.com/userguide3/database/results.html
1.m_login->cek_login 1.m_login-> cek_login
this functions returns a SQL QUERY OBJECT 该函数返回一个SQL QUERY OBJECT
2.the SQL QUERY OBJECT ->num_rows() function returns a INT ($cek) 2.SQL QUERY OBJECT-> num_rows()函数返回INT($ cek)
3.so $cek is a INT . 3.so $ cek是一个INT。 so u cant use like an array ($cek['username'])
所以你不能像数组一样使用($ cek ['username'])
so Please try this code 所以请尝试这段代码
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => md5($password)
);
$cek = $this->m_login->cek_login("admin",$where);//$cek is a Query Object
$rows = $cek->num_rows();//$rows is a INT
if($rows > 0){
$cek = $cek->row_array();//now u get an Array
$data_session = array(
'username' => $cek['username'],
'nama' => $cek['nama']
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.