Reputation:
i am new to CodeIgniter and facing problem in adding user id(after user has logged in) from database to session data here is my code question may be asked before on SOF , after putting all my efforts i am asking this
//login-model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//get the username & password from tbl_usrs
public function get_user($usr, $pwd)
{
$sql = "select * from tbl_usrs where username = '" . $usr . "' and password = '" .$pwd . "' ";
$query = $this->db->query($sql);
return $query->num_rows();
}
/* public function set_session($username) {
$sql="SELECT * FROM tbl_usrs WHERE username='".$username."' LIMIT 1 ";
$result=$this->db->query($sql);
$row=$result->row();
$sess_data=array (
'id'=>$row->id,
'username'=>$username,
'is_login'=>TRUE
);
$this->session->set_userdata($sess_data);
} //set_seesion function ends
*/
}
?>
//login controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
print_r( debug_backtrace() );
//ini_set('memory_limit', '-1');
//ini_set('max_execution_time', 3000);
ini_set('display_errors',1);
error_reporting(E_ALL);
class LoginController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
}
public function index()
{
//load the login model
$this->load->model('login_model');
// $qry=$this->login_model->validate();
//get the posted values
$username = $this->input->post("username");
$password = $this->input->post("password");
$user_id = $this->input->get("user_id");
//set validations
$this->form_validation->set_rules("username", "username", "trim|required");
$this->form_validation->set_rules("password", "password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
//validation fails
redirect('Homecontroller/index'); //make new controller for loading a form again
echo "Validation fails"; // even for no name and passwrd
}
else
{
//validation succeeds
if ($this->input->post('submit') == "Login")
{
//check if username and password is correct
$usr_result = $this->login_model->get_user($username, $password);
if ($usr_result > 0) //active user record is present
{
//set the session variables
$sessiondata = array(
'user_id' => $user_id,
'username' => $username,
'password'=>$password,
'is_login' => TRUE
);
//$this->login_model->set_session($username);
$this->session->set_userdata($sessiondata);
print_r($this->session->all_userdata()); //to check
redirect(base_url("Site/member_area"));
return $username;
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
redirect('Homecontroller/index');
}
}
else
{
redirect('login/home3');
}
}
}
}?>
when i am try to print session data in view i am getting user_id=> empty
Upvotes: 3
Views: 1955
Reputation: 50787
None of these variables will be available to you for assignment:
'user_id' => $user_id,
'username' => $username,
'password'=>$password,
The reason for this is that you've used:
$usr_result = $this->login_model->get_user($username, $password);
However, that is only going to be the result of the number of rows returned:
public function get_user($usr, $pwd)
...
return $query->num_rows();
So now, $usr_result
will be the number of rows returned, not the data of the user in question.
So, what you should do, is instead set the session data in that login function above return $query->num_rows();
and below $query=
.
public function get_user($usr, $pwd){
....
$query = $this->db->query($sql);
$this->session->set_userdata('sessiondata', $query->row_array());
}
Note that as we only retrieve 1 user from our query ever, we also would only ever want to get 1 row back. Furthermore, you're wanting to store an associative array, and not an object, so we use $query->row_array()
to get back 1 row
formatted as an array
where the key
is the column name and the value is the columns result.
Now, do a echo '<pre>', print_r($this->session->userdata('sessiondata'), true),'</pre>';
to get a formatted list of that object which contains the array. Then you can map names accordingly.
Sidenote, i would not store the password in an session.
Upvotes: 0