Include db entry in session data when logging user in (CodeIgniter) - php

What I'd like to do is on validating a CI user, when setting their session data, pull some data from a field set in the db.
At the moment I have this within my controller;
function validate()
{
$query = $this->model_auth->validate();
if ($query) // if the user's credentials validated
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('dashboard/');
} else {
$data['error'] = 'Invalid User Id and Password combination';
$this->load->view('view_login',$data);
}
}
and this is my model
function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1) { return TRUE; }
}//validate
What I'd like to do is set something like this;
$data = array(
'username' => $this->input->post('username'),
'user_level' => $ThisIsSomethingWithinTheDB,
'is_logged_in' => TRUE
);
Within my controller, with $ThisIsSomethingWithinTheDB relating to the relevant entry within the db under the user_level column.
I'm currently still very much learning as I go so any help would be very much appreciated.

If you want to set something equal to something from your database you're going to have to retrieve it first.
I'm not too familiar going with CI's ActiveRecord as I prefer to use Laravel and Eloquent but from what I can tell looking at the docs:
$this->db->get('users')
... would be equivalent to:
SELECT * FROM users
and if that's the case, you should be able to set:
$data['user_level'] = $ThisIsSomethingWithinTheDB
or
$this->session->set_userdata('user_level') = $ThisIsSomethingWithinTheDB

Modify your validate() function, to return the DB results.
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
if ( $query->num_rows() > 0 )
{
$result = $query->result();
return $result[0];
}
else
{
return false;
}
}
Now, you can access the DB results, like this in your controller...
if ($query)
{
$data = array(
'username' => $this->input->post('username'),
'user_level' => $query->user_level, // This is "user_level" row from your DB
'is_logged_in' => true
);
}
Hope this helps.

Thanks for your help both,
I was able to achieve this as follows;
model;
function validate() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return $query;
}
else
{
return FALSE;
}
}
Controller;
function validate()
{
$query = $this->model_auth->validate();
if ($query) // if the user's credentials validated
{
$user_data = $query->row();
$data = array(
'username' => $this->input->post('username'),
'user_level' => $user_data->user_level,
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('dashboard/');
} else {
$data['error'] = 'Invalid User Id and Password combination';
$this->load->view('view_login',$data);
}
}
Thanks again for your help!

Related

How can I display the name of my admin full name after login - Codeigniter

I am having a trouble on displaying my admin's full name, like for example admin full name is John Doe, it is not displaying. I am still learning codeigniter please give me advice thank you!
here is my controller
//Get username
$username = $this->input->post('username');
//Get and encrypt the password
$password = $this->input->post('password');
// Login user
$user_id = $this->role_model->login($username, $password);
if ($user_id) {
// Create session
$user_data = array(
'user_id' => $user_id,
'name' => $user_id->name,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
//Set message
$this->session->set_flashdata('user_loggedin','You are now logged in');
redirect('pages/index');
here is my View - where I would like to display my full name, as you can see 'name' is the data field I have to display but it is does not show anything, it gives an error that says name is not defined.
<li><a> Welcome, <?php echo $this->session->name ?> </a></li>
Model
public function login($username, $password){
//Validate
$this->db->where('username',$username);
$this->db->where('password',$password);
$result = $this->db->get('users');
if ($result->num_rows() == 1) {
return $result->row(0)->id;
}else{
return false;
}
}
Your method login() returns only id = digit (return $result->row(0)->id;), not object (in controller your get $user_id->name).
Do this, in the model:
if ($result->num_rows() == 1) {
return $result->row(0); // fix
}else{
return false;
}
In the controller:
$user = $this->role_model->login($username, $password);
$user_data = array(
'user_id' => $user->id, // fix
'name' => $user->name, // fix
'username' => $username,
'logged_in' => true
);
It shows undefined because it is indeed undefined.
In your view your're trying to echo the returning value of a function called userdata(), does that function actually exist? Or is $this->session->userdata an array? in which that case, you need to use [ index ] to acces a member within an array. i.e. $this->session->userdata['name'], but also, that member needs to exist first.
controller :
if($this->Login_model->login_valid($email,$password))
{
$sessiondata = array(
'username' => $email
);
$this->session->set_userdata('logged_in', $sessiondata);
redirect('narson');
}
else
{
$this->session->set_flashdata('error',"Invalid Username And password");
redirect('login');
}
model:
public function login_valid($username,$password)
{
$this->db->where('username',$username);
$this->db->where('password',$password);
$query = $this->db->get('admin');
if($query->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}

setting sessions to use codeigniter

OK I am trying to build a user profile where there first_name, password and username are shown in the view.
because password and username are set in the model its very easy to set the session userdata in the controller then have it shown in the view.
how could I also set a sessions to get first_name without puting it in the validation function ?
thank you, im new to codeigniter and MVC,
thanks for any help.
Model
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows() == 1)
{
return true;
}
}
controler
function validate_credentials()
{
$this->load->model('member_model');
$query = $this->member_model->validate();
if ($query) // if user cred validate the user session start
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('members/members_area');
} else {
$this->index();
echo 'Incorrect Password or Username';
}
}
view
<h2>Welcome Back, <?php echo $this->session->userdata('username'); ?>!</h2>
<h2>your password, <?php echo $this->session->userdata('password'); ?>!</h2>
Validation Function
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows() == 1)
{
return $query->row();
}
else
{
return false;
}
}
In Controller
function validate_credentials()
{
$this->load->model('member_model');
$query = $this->member_model->validate();
if ($query) // if user cred validate the user session start
{
$data = array(
'username' => $query->username,
'password' => $query->password,
'first_name'=>$query->firstname
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('members/members_area');
} else {
$this->index();
echo 'Incorrect Password or Username';
}
}

CodeIgniter: How to return all data in the tables row after valitdaion?

I have this public function in the model folder that get inputs from a controller of a username and a password:
// Validate that the user is registered and returns true or false.
public function validate($email, $password) {
$this->db->select('u_email', 'u_password');
$this->db->from('users');
$this->db->where('u_email', $email);
$this->db->where('u_password', $password);
$query = $this->db->get();
if ( $query->num_rows() == 1 ) {
return $query->result();
} else {
return FALSE;
}
}
I want it to return all row information that belong to this username+password (there is a u_id, u_email, u_name, u_permissions).
So I can use in the controller and save in a session:
$this->load->model('membership_model');
$query = $this->membership_model->validate($email, $password);
if ( $query ) { // if the user's credentials validated...
$data = array(
'u_id' => $query->u_id,
'email' => $query->u_email,
'name' => $query->u_name,
'permittions' => $query->u_permittion,
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
Just use * to fetch all data and use row() to get single fetch row
$this->db->select('*');// use * here
$this->db->from('users');
$this->db->where('u_email', $email);
$this->db->where('u_password', $password);
$query = $this->db->get();
if ( $query->num_rows() == 1 ) {
return $query->row();// use row();
} else {
return FALSE;
}
Read https://www.codeigniter.com/user_guide/database/examples.html

if else is not working

The model:
function validate()
{
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('memberships');
if($query->num_rows() == 1)
{
return TRUE;
}
}
function validate_admin()
{
$this->db->where('adminname',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('admin');
if($query->num_rows() == 1)
{
return TRUE;
}
}
The controller
function validate_credentials()
{
$this->load->model('membership_model');
if($this->membership_model->validate())
{
$this->db->where('username',$this->input->post('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() > 0){
foreach ($get_profile_info->result() as $row)
{
$info = array('firstname' => $row->firstname,
'lastname' => $row->lastname,
'email_address' => $row->email_address
);
}
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'firstname' => $info['firstname'],
'lastname' => $info['lastname'],
'email_address' => $info['email_address'],
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('/site/main_menu');
}}
else if($this->membership_model->validate_admin())
{
echo "admin";
}
else
{
redirect('login');
}
}
The if else if statement is not working correctly. The program test the first condition and if it returns false skips the second condition even if that is TRUE and execute the else statement. I'm not sure what is going worng here.
Refactor your one controller method into different methods - one for Members and one for Admin to start. And because you are calling separate database tables would suggest having a separate model for each.
Because you are interacting with a database table getting the profile information should happen in a model (not the controller).
This is a personal preference but i would set the session data in a model as well. Also there might be issues with your foreach and then getting the value $info['first name'].
Validate the form data first before sending to your database. Its safer and its better for your users - if they forget to put in the password you want to show them the form again with a helpful message. http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
and remember when in doubt -- echo it out.

Codeigniter giving unusual results on login controller

I am using codeigniter framework. I have a login model and a controller with a view. I have admin as username and admin as password set in the users table in my db for test purpose.
But when ever I use admin13356 or admindsgsd or adminWHATEVER with admin as password, the login is successful. I dont understand why.
My controller function is as follows.
function check(){
$this->load->model('loginModel');
$query = $this->loginModel->validate();
if($query){
$data = array('username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
//redirecting to appropriate page
redirect('success');
}else{
$this->session->set_flashdata('loginCheck','Username/Password Comination Incorrect!');
redirect('login');
}
}
And my model is as below.
function validate(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows() == 1){
return true;
}
}
function validate(){
// dump all post variables recieved
echo '<pre>';
print_r($this->input->post());
echo '</pre><br>';
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
// Dump what the query result is
echo '<br><pre>';
print_r($query->result());
echo '</pre>';
//if($query->num_rows() == 1){
// return true;
//}
}
Your code seems fine, but can you check if it is passing the right data correctly try dumping it first. like the code above to check the data and the query result.
how many records do you have in the table
try this code
function validate($username,$password){
$this->select('*');
$this->from('table_name');
$this->db->where('username', $username);
$this->db->where('password', md5($password));
$query = $this->db->get();
if($query->num_rows() == 1){
return true;
}
}
pass your username and password to the function
What version of PHP are you running? I've had problems with PHP before where it returns TRUE for non returns.
Try adding return false to your validate() method:
function validate(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('users');
if($query->num_rows() == 1){
return true;
}
return false;
}
Also, Model's should be input agnostic. Definitely think about moving the $this->input->post() to the controller and pass the values in as parameters:
This allows you to use the validate() method in other scenarios. For example (re-validating the password when the username isn't supplied via post but via session).
Controller
function check(){
$this->load->model('loginModel');
$query = $this->loginModel->validate($this->input->post('username'), $this->input->post('password'));
if($query){
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
//redirecting to appropriate page
redirect('success');
}else{
$this->session->set_flashdata('loginCheck', 'Username/Password Combination Incorrect!');
redirect('login');
}
}
Model
function validate($username, $password){
$this->db->where('username', $username);
$this->db->where('password', md5($password));
$query = $this->db->get('users');
if($query->num_rows() == 1){
return true;
}
return false;
}

Categories