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.
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;
}
}
Code-igniter Session is not storing data, it only saves the Email which we are entering in input box in login-form. I set this $config['sess_use_database'] = TRUE; and created a ci_sessions table also in DB but no sake. I tried this on both versions on code-igniters(2 & 3) Please help me to sort out this problem. Thank you. This is my code. First one is controller and the second one is model.
public function validate_credentials()
{
$this->load->model('passenger_login_model');
$query = $this->passenger_login_model->validate();
if($query)
{
$data = array(
'Email' => $this->input->post('Email'),
'is_logged_in' => true,
'P_ID' => $query->P_ID,
'CNIC' => $query->CNIC
);
$this->session->set_userdata($data);
echo $this->session->userdata('Email');
echo $this->session->userdata('P_ID');
//redirect('front');
}
else
{
echo "<script>alert('Incorrect Email or Password!');</script>";
$this->index();
}
}
and as output, it only shows me the user#user.com not P_ID
and this is model
class Passenger_Login_model extends CI_Model
{
/*`passenger`(`P_ID`, `Name`, `Image`, `CNIC`, `Passport_No`, `Gender`, `Email`,
`Password`, `Phone_No`, `Mob_No`, `Address_1`, `Address_2`, `Date` */
function validate()
{
$data=array(
'Email' =>$this->input->post('Email'),
'Password' =>$this->input->post('Password')
);
$rec=$this->db->get_where('passenger', $data)->result();
$c = count($rec);
if($c>0)
{
return true;
}
else
{
return false;
}
}
function is_logged_in()
{
$this->load->library('session');
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page.'.anchor('login',"Login");
die();
}
}}
You should return the data fetch from your model query, use fetch function to do that, for more reference click, So your model validate function should be
function validate()
{
$data=array(
'Email' =>$this->input->post('Email'),
'Password' =>$this->input->post('Password')
);
$rec=$this->db->get_where('passenger', $data);
return $rec->result_array(); // returns data as array // might help you
}
return $res->result_array(); will return multipdimentional array of data from db, to know more about result_array() check this
Session issue is from your controller function the result is array now.
public function validate_credentials()
{
$this->load->model('passenger_login_model');
$result_data = $this->passenger_login_model->validate();
// result data is array now
if(is_array($result_data) && count($result_data) > 0)
{
// var_dump($result_data); // check this var_dump you will get multidimensional array
$data = array(
'Email' => $this->input->post('Email'),
'is_logged_in' => true,
'P_ID' => $result_data[0]['P_ID'],
'CNIC' => $result_data[0]['CNIC']
);
$this->session->set_userdata($data);
echo $this->session->userdata('Email');
echo $this->session->userdata('P_ID');
//redirect('front');
}
else
{
echo "<script>alert('Incorrect Email or Password!');</script>";
$this->index();
}
}
Hope this help, Happy coding.
My problem solved, i just updated the model to this...
function validate()
{
$data=array(
'Email' =>$this->input->post('Email'),
'Password' =>$this->input->post('Password')
);
$query = $this->db->get('passenger');
if ($query->num_rows())
{
return $query->row();
}
}
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!
I have a problem with the function of the mode. When I put the right admin name and admin password, it doesn't work. Instead, it gives me the last error message ("You don't have..."), but if I comment out the second WHERE clause in my model, then it works as intended.
Model:
function validate_admin()
{
$this->db->where('adminname',$this->input->post('adminname'));
//$this->db->where('adminpassword',md5($this->input->post('adminpassword')));
$query = $this->db->get('admin');
if($query->num_rows() == 1)
{
return TRUE;
}
}
Controller:
function validate_admin_credentials()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('adminname','Username','trim|required');
$this->form_validation->set_rules('adminpassword','Password','trim|required');
if($this->form_validation->run() == FALSE)
{
$data['main_content']='login_failed';
$this->load->view('includes/template',$data);
}
else
{
$this->load->model('admin_model');
if($this->admin_model->validate_admin())
{
$data = array(
'adminname' => $this->input->post('adminname'),
'adminpassword' => $this->input->post('adminpassword'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('/site/admin_panel');
}
else
{
echo "You don't have administration privileges";
}
}
}
How can I resolve this issue?
If you completely sure about password you can try this code:
$this->db->where('adminpassword', md5($this->input->post('adminpassword', TRUE)));
Edit: edited, sorry for my poor english
You can try it.
Model
$adminpassword = md5($this->input->post('adminpassword'));
$data = array('adminname' => $this->input->post('adminname'),
'adminpassword' => $adminpassword);
$query = $this->db->get_where('admin', $data);
return $query->num_rows();
Controller
if($this->admin_model->validate_admin() > 0)
I have created a Facebook App that i need people to only enter their data to once.
It's all working and the database is being populated, but i need to make sure people don't keep coming back and re-entering their data endlessly.
What's the best way to check if the user has already submitted their data ?
The signed_request could still have been submitted and their data not entered so i need the check for both to work.
Ideally the PHP would just check for FB ID + other data, and only display a confirmation / thankyou page.
Currently my php to send to the database is:
class Users_Model extends CI_Model {
protected $_name = 'users';
function add($id,$key,$value) {
$data = array(
'id' => $id,
'name' => $key,
'value' => $value
);
return $this->db->insert($this->_name, $data);
}
function update($id,$key,$value) {
$data = array(
'value' => $value
);
$this->db->where(array(
'id' => $id,
'name' => $key
));
return $this->db->update($this->_name, $data);
}
function exists($id,$key=null) {
if($key == null) {
$this->db->where(array(
'id' => $id
));
} else {
$this->db->where(array(
'id' => $id,
'name' => $key
));
}
$query = $this->db->get($this->_name);
if($query->num_rows() > 0) {
return true;
}
return false;
}
function remove($id) {
$data = array(
'id' => $id,
);
return $this->db->delete($this->_name, $data);
}
function all() {
$query = $this->db->get($this->_name);
$results = array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$results[]=$row;
}
}
return $results;
}
}
Any help much appreciated...
What's the best way to check if the user has already submitted their data ?
Check if you already have a record for the user’s Facebook id in your database.