I'm trying to display the user's firstname after login but it doesn't appear on the page. I tried to display it like this
<h2> Welcome <?php echo $this->session->userdata('firstname'); ?> </h2>
but it didn't display the user's firstname, even though there are data in DB. But when I tried to change the 'firstname' into 'username', it displayed the email of the user(test#gmail.com). How can I display the user firstname? Here's my controller:
function login(){
$this->form_validation->set_rules('username', 'Username', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('/');
} else {
$user_login = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
);
$login = new UI_model;
$result = $login->login_user($user_login);
if($result){
$auth_details = array(
'firstname' => $result->firstname,
'lastname' => $result->lastname,
'username' => $result->username,
);
$this->session->set_userdata($auth_details);
// $this->session->set_userdata('authenticated', '1');
$this->session->set_flashdata('status', 'User Logged in Successfully!');
redirect(base_url('pages/index'));
} else {
$this->session->set_flashdata('status', 'Invalid Credentials. Please try again');
redirect(base_url('pages/about'));
}
}
}
Here's my Model:
public function login_user($data){
$this->db->select('*');
$this->db->where('username', $data['username']);
$this->db->where('password', $data['password']);
$this->db->from('users');
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
} else {
return false;
}
}
Can you attach an image of your database you are using for and echo all data session?
Related
i'm trying to make a login system. When user already login it will show user personal data like name, email or something. I'm try to modify the login code but doesn't work. Maybe someone can help.
Here is my code.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_user');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('user/user_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password,
'status' => 1
);
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
}else{
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
}
function logout(){
$this->session->sess_destroy();
redirect('user/user_login');
}
}?>
Model
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
I'm can make username session appear,
<?php echo $this->session->userdata('username') ?> it's working.
But try to show name it doesnt appear. Appreciate any kind help. Thank you
You can try something like this.
$query = $this->Model_user->cek_login('tb_user',$where);
if($query->num_rows() > 0){
$user = $query->row();
$data_session = array(
'username' => $username,
'status' => "userlogin",
'name' => $user->name,
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
} {
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
For this to work, there must be a name column in the table tb_user
You need to store the full name in the session too.
Try editing this
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
to this
$cek = $this->Model_user->cek_login('tb_user',$where);
if($cek->num_rows() == 1){
$row=$cek->result_array()[0]; //returns an array of results, pick the first
$data_session = array(
'fullname' => $row['fullname'], //assuming column name is 'fullname'
'username' => $username,
'status' => "userlogin",
);
Now I'm developing system documentation using Codeigniter. But the problem is Not logged in Problems switching between pages. login and Welcome To switch on like this forever.
public function index()
{
if($this->input->post(NULL)){
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->m_login->check_login($username,$password);
if(!empty($result)){
$data_account = array();
foreach($result as $row){
$data_account = array(
'account_id' => $row->id,
'username' => $row->user_name,
'password' => $row->password,
'fullname' => $row->fullname,
'email' => $row->email,
'tel' => $row->tel,
'detail' => $row->detail
);
}
$this->session->set_userdata('data_account',$data_account);
$this->__login();
//create_log_activity($this->account_id, date("Y-m-d H:i:s"));
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}else if($this->session->userdata('data_account')){
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}
public function logout(){
$this->session->unset_userdata('data_account');
redirect('','refresh');
}
I want to know how to solve the problem, thanks in advance.
Use
if($_SERVER['REQUEST_METHOD'] == "POST"){
instead of
if($this->input->post(NULL)){
I want to add condition for my login form - if a user is not already active, not to log in. I'm using CodeIgniter. That's my controller:
public function login ()
{
$this->load->model('user_model');
$user=$this->user_model->login();
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_login_check');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run()==FALSE)
{
$this->index();
}
else
{
if(count($user) > 0 )
{
$this->load->library('session');
$data = array(
'username' => $user['username'],
'user_id' => $user['user_id'],
'is_logged_in' => TRUE,
'role_id' => $user['role_id']
);
$this->session->set_userdata($data);
redirect('index/home_page');
}
}
}
My model is:
public function login()
{
$this->db->select('*');
$this->db->from('users');
$this->db->where('username', $this->input->post('username'));
$this->db->where('password',sha1($this->input->post('password')));
//$this->db->where('deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL');
$result=$this->db->get();
return $result->row_array();
}
I have tried with this: $this->db->where('deactivated_at = "0000-00-00 00:00:00" OR deactivated_at IS NULL'); in my login function, but it does not work. How could I make this authentication, if not active user, not to log in at all?
There's a few things I would say is wrong with your code.
First off, you're trying to login the user before the form validation has completed. This should be done afterwards, or I don't see the need for validation?
This would be my version of your login function, within your controller.
function login()
{
$this->load->model('users_model');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if (!$this->form_validation->run())
{
$this->index(); // Show the login form..
}
else
{
// This is where we try to login the user, now the validation has passed
if ($user = $this->users_model->login())
{
// Start the session...
}
else
{
// The model returned false..
}
}
}
So you don't go to the model, until the form validation has passed. Then, in your model;
function login()
{
$where = array(
'username' => $this->input->post('username'),
'password' => sha1($this->input->post('password'))
);
// $this->db->select('*'); No need for this line
$query = $this->db->get_where('users', $where);
if ($query->num_rows() > 0)
{
// Found a match
// Are they activated?
if (!is_null($query->row('deactivated_at'))
{
// The user isn't deactivated
return $query->row_array();
}
else
{
// The user is deactivated
return false;
}
}
else
{
// The username and/or password is wrong...
return false;
}
}
Hope this helps.
I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site
I'm trying to update a contact and only update the contact depending on the id.
Model:
function updateContact() {
$data = array(
'title' => $this->input->post('title'),
'first_name' => $this->input->post('first_name'),
'surname' => $this->input->post('surname'),
'phone_number' => $this->input->post('phone_number'),
'fax' => $this->input->post('fax'),
'email' => $this->input->post('email')
);
$this->db->where('id', $this->uri->segment(3));
$this->db->update('contacts', $data);
}
Controller:
function update() {
$this->load->model('league/add_contact_model');
$data['rows'] = $this->add_contact_model->viewContact();
// field name, error message, validation rules
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('surname', 'Surname Name', 'trim|required');
$this->form_validation->set_rules('phone_number', 'Phone Number', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = "league/update_contact";
$this->load->view('includes/template', $data);
}
else
{
if($query = $this->add_contact_model->updateContact())
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/contact_updated');
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/about_added');
$this->load->view('includes/footer');
}
}
}
It's updating if the where statement isn't there, but obviously it just updates everything, so just unsure of how the where statement should be written.
Thanks in advance for any help :)
function updateContact( $id ) {
....
$this->db->where('id', $id );
in your controller make sure that id (segment(3)) is valid before execute Model method
Just to add to bensius comment, you really shouldn't have $this->uri->segment(3) in the model. This should belong in your controller.