Codeigniter session data or database query?

2019-08-01 22:11发布

问题:

How can I show customer's or user's information such as Name and Surname on the header?

Firstly, I have a login page. Customer and User can login to the dashboard on the same login page. While login process, it uses 2 database tables such as User and Customer. Login process system takes data from 2 database tables when customer and user login to dashboard.

Model of LOGIN:

private $table = "user, customer";
    private $_data = array();

    public function validate()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        // Username or Email Sending Settings
        $this->db->where("(user.userEmail = '$username' OR user.userUsername = '$username') OR (customer.cosEmail = '$username' OR customer.cosUserName = '$username')"); //
        $query = $this->db->get($this->table);

        if ($query->num_rows())
        {
            // found row by username    
            $row = $query->row_array();

            // now check for the password
            if ($row['userPass'] == $password OR $row['cosPassword'] == $password)
            {
                // we not need password to store in session
                unset($row['userPass' OR 'cosPassword']);
                $this->_data = $row;
                return ERR_NONE;
            }

            // password not match
            return ERR_INVALID_PASSWORD;
        }
        else {
            // not found
            return ERR_INVALID_USERNAME;
        }
    }

    public function get_data()
    {
        return $this->_data;
    }

And I want to show information of customer or user on the header. When customer login, the customer can see own information or when user login, the user can see own information in the same dashboard.

For this, I use SessionData of Codeigniter. View Header:

<li class="dropdown">
                <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                    <img alt="" src="<?php echo base_url().'upload/user/'.$this->session->userdata('userImg'); ?>">
                    <span class="username"><?php echo $this->session->userdata('userName'); ?>  <?php echo $this->session->userdata('userSurname'); ?></span>
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu extended logout">
                    <div class="log-arrow-up"></div>
                    <li><a href="<?php echo base_url(); ?>profile"><i class=" fa fa-suitcase"></i>Profile</a></li>
                    <li hidden><a href="#"><i class="fa fa-cog"></i> Settings</a></li>
                    <li hidden><a href="<?php echo base_url(); ?>notifications"><i class="fa fa-bell-o"></i> Notification</a></li>
                    <li><a href="<?php echo base_url(); ?>logout"><i class="fa fa-key"></i> Log Out</a></li>
                </ul>
            </li>

But I can't show anything when customer login. Because I don't know how can I show customer information when customer login. When customer login dashboard, I see user information again. How can I set this what I want? Help me, It is so important.

I tried it:

$this->session->userdata('userName') || $this->session->userdata('cosName')

But system shows me 1 on username. It doesn't work.

What can I show information according to customer or user with userdata?

My Login Controller:

public function index()
    {   
        $this->logged_in_check();

        $this->load->library('form_validation');
        $this->form_validation->set_rules("username", "Username", "trim|required");
        $this->form_validation->set_rules("password", "Password", "trim|required");
        if ($this->form_validation->run() == true) 
        {
            $this->load->model('login_model', 'login'); 
            // check the username & password of user
            $status = $this->login->validate();
            if ($status == ERR_INVALID_USERNAME) {
                $this->session->set_flashdata("error", "Username or Email is invalid");
            }
            elseif ($status == ERR_INVALID_PASSWORD) {
                $this->session->set_flashdata("error", "Password is invalid");
            }
            else
            {
                // success
                // store the user data to session
                $this->session->set_userdata($this->login->get_data());
                $this->session->set_userdata("logged_in", true);
                // redirect to dashboard
                redirect("dashboard");
            }
        }   
        $this->load->view("login");
    }

回答1:

You can use just this function and remove other function.

 public function index(){   
    $this->load->library('form_validation');
    $this->form_validation->set_rules("username", "Username", "trim|required");
    $this->form_validation->set_rules("password", "Password", "trim|required");
    if ($this->form_validation->run() == FALSE) 
    {
        $this->session->set_flashdata("error", validation_errors());
        $this->load->view("login");
    }
    else{
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user_data = $this->db->query("(SELECT * FROM user WHERE  userUsername = '$username' OR userEmail = '$username' AND userPass = '$password')");
        $cus_data = $this->db->query("(SELECT * FROM customer WHERE  cosUserName = '$username' OR cosEmail = '$username' AND cosPassword = '$password')");
        if ($user_data->num_rows() > 0) {
            foreach ($user_data->result_array() as $row) {
                $this->session->set_userdata('people_name',$row['userUsername']);
                $this->session->set_userdata("logged_in", true);
            }
            // redirect to dashboard
            redirect("dashboard");
        }
        elseif ( $cus_data->num_rows() > 0) {
            foreach ($cus_data->result_array() as $row) {
                $this->session->set_userdata('people_name',$row['cosUserName']);
                $this->session->set_userdata("logged_in", true);
            }
            // redirect to dashboard
            redirect("dashboard");
        }
        else{
            $this->session->set_flashdata("error", "Username or Email is invalid");
            $this->load->view("login");
        }
    }  
}

also in view use this code echo $this->session->userdata('people_name') to show user or customer name.