This is the code work fine but only for one column search which is user in below code, but need to search more than one column like password , email
controller
public function index()
{
$this->input->post('search'); //this is drag from view table *name=search*
$search=$this->input->post('search');
$data['user']=$this->usermodel->searchUser($search);
$this->load->view('user',$data);
}
model
public function searchUser($search=null)
{
$this->db->select("*");
$this->db->from("tbl_user");
$this->db->where("user",$search); //user is one column of table *tbl_user*
$query=$this->db->get();
if($query->num_rows()>0)
{
$result=$query->result_array();
return $result;
} else {
return false;
}
}
You can use multiple where like this
$this->db->where('email',$email);
$this->db->where('password',$password);
You can use an array and pass the array.
Associative array method:
$array = array('user' => $user, 'email' => $email, 'password' => $password);
$this->db->where($array);
// Produces: WHERE user = 'Joe' AND email = 'joe#example.com' AND password = 'password'
Please check CI Query Builder Class for details
Related
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;
}
}
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
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!
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.
Trying to put together a simple login authentication. Been at this for quite sometime, and I can't find where I'm going wrong. Pretty new to Codeigniter and OOP PHP. I know there are authentication libraries out there, but I'm not interested in using those.
Model:
function login($username, $password){
if ($this->db->table_exists($username)){
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get($username);
if($query->num_rows >= 1)
{
return true;
$data = array(
'username' => $this->input->post('username'),
'login' => true
);
$this->session->set_userdata($data);
}
}
}
Controller
function __construct(){
parent::__construct();
$this->logincheck();
}
public function logincheck(){
if ($this->session->userdata('login')){
redirect('/members');
}
}
If I just echo from the controller: $this->session->all_userdata(); I get an empty array. So the problem seems to be that the $data array in the model isn't being stored in the session.
This code is part of your problem. You're asking the database driver if a table with the same name as the username exists, and also trying to get the username and password from the same table.
if ($this->db->table_exists($username)){
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get($username);
Change the $username in $this->db->table_exists($username); and $this->db->get($username); to whatever table your users are in.
I am guessing your login function is the issue:
if ($this->db->table_exists($username)){
This just doesn't look right... If your user name was admin, there there would need to be an admin table, etc... I am posting a quick example I recently wrote that seems to get the job done:
public function doLogin(){
$this->db->select('user_pass,user_id');
$query = $this->db->get_where('users', array('user_name' => $this->input->post('login')));
if($query->num_rows<1 || $query->row('user_pass')!=md5($this->input->post('password')))
return false;
$this->session->set_userdata(array('loggedin'=>true,'user_id'=>$query->row('user_id')));
return true;
}