PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] how to show user' name after login in codeigniter

 January 18, 2022     codeigniter, php     No comments   

Issue

hiii, My website contains two mode...user and admin in a codeigniter if a user login in his account, The name of user should be displayed in a dashboard. i know how to do it core php..but in codeigniter please help

user.php(controller)

public function  userloginvalidation()
    {
        $email = $this->input->post('email');
        $password = $this->input->post('password');
  
        $this->load->model('loginmodel');
        $id = $this->loginmodel->isvalidateuser($email, $password);
        if ($id) {
            $this->load->library('session');
            $this->session->set_userdata('id', $id);
            return redirect('user/userdashboard');
        } else {
            $this->load->library('session');
            $this->session->set_flashdata('loginfailed', 'invalid username or password');
            return redirect('user/userlogin');
        }      
    }

loginmodel.php(model)

public function isvalidateuser($email,$password)
    {
        $q = $this->db->where(['email'=>$email,'password'=>$password])
                    ->get('user');

                    if($q->num_rows())
                    {
                       
                        return $q->row()->id;
                      
                    }

                    else
                    {
                        return false;
                    }
    }


login.php(view)

<?php echo form_open('user/userloginvalidation'); ?>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <?php echo form_input(['type'=>'email','class'=>'form-control','id'=>'exampleInputEmail1','placeholder'=>'Enter your Email','name'=>'email','aria-describedby'=>'emailHelp','value'=>set_value('email')]); ?>
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <?php echo form_password(['required','type'=>'password','class'=>'form-control','id'=>'exampleInputPassword1','placeholder'=>'password','name'=>'password']); ?>
  </div>
  <?php echo form_submit(['value'=>'Login','class'=>'btn btn-info submitbtn','type'=>'submit']) ?>
</form>

please help me how to do it!!!!!! thankyou


Solution

Get the data(name) from your table with the help of id, then you can set it as a session and print it in your view file, like so -

Controller

public function  userloginvalidation()
{
    $email    = $this->input->post('email');
    $password = $this->input->post('password');

    $this->load->model('loginmodel');
    
    $id = $this->loginmodel->isvalidateuser($email, $password);
    
    if ($id) {
   
        // You probably want to do this in model ↓↓
        $userName = $this->db->select('name')->from('user')->where('id', $id)->get()->row(); // get the name from the table

        $this->load->library('session'); // autoloading the session library is a good idea
        
        // set the session 
        $this->session->set_userdata('id', $id);
        $this->session->set_userdata('name', $userName->name); // or simply $_SESSION['name'] = $userName->name;
        
        return redirect('user/userdashboard');
    
    } else {
        
        $this->load->library('session');
        $this->session->set_flashdata('loginfailed', 'invalid username or password');
        
        return redirect('user/userlogin');
    }      
}

View

<?php 
    echo $this->session->userdata('name');  // or simply echo $_SESSION['name'];
?>

See if it helps you.



Answered By - sauhardnc
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing