Home How to shorten array on get all user session data form table database?
Reply: 0

How to shorten array on get all user session data form table database?

user3888
1#
user3888 Published in May 22, 2018, 11:24 pm

I have a problem to showing all user session data in PHP MySQL (Codeigniter). For example, when I login by entering a username and password then the admin details page will show all my data.

Controller: Login.php

<?php
class Login extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('datalogin');
    }

    function index(){

        $this->load->view('login');
    }

    function check(){
        $username=htmlspecialchars($this->input->post('username',TRUE),ENT_QUOTES);
        $password=htmlspecialchars($this->input->post('password',TRUE),ENT_QUOTES);

        $cek_user=$this->datalogin->auth_user($username,$password);

        if($cek_user->num_rows() > 0){
                $data=$cek_user->row_array();
                $this->session->set_userdata('good',TRUE);
                 if($data['level']=='10'){ //Administrator
                        $this->session->set_userdata('akses',$data['level']);
                        $this->session->set_userdata('session_id',$data['id']);
                        $this->session->set_userdata('session_name',$data['name']);
                        $this->session->set_userdata('session_username',$data['username']);
                        $namalevel = "Administrator";
                        $this->session->set_userdata('session_level',$level);
                        $this->session->set_userdata('session_foto',$data['foto']);
                        redirect('adminpage');
                     } if($data['level']=='1'){ //Leader
                        $this->session->set_userdata('akses',$data['level']);
                        $this->session->set_userdata('session_id',$data['id']);
                        $this->session->set_userdata('session_name',$data['name']);
                        $this->session->set_userdata('session_username',$data['username']);
                        $namalevel = "Leader";
                        $this->session->set_userdata('session_level',$level);
                        $this->session->set_userdata('session_foto',$data['foto']);
                    redirect('leaderpage');
                } else {
                    redirect('login/logout');
                }

        } else {
            $this->load->view('login');
        }
    }

    function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}

This problem is writing very long arrays:

$this->session->set_userdata('akses',$data['level']);
$this->session->set_userdata('session_id',$data['id']);
$this->session->set_userdata('session_name',$data['name']);
$this->session->set_userdata('session_username',$data['username']);
$namalevel = "Administrator";
$this->session->set_userdata('session_level',$level);
$this->session->set_userdata('session_foto',$data['foto']);
//etc....................

is it possible to write down all the data if I have so much data of self? is there another shorter way?

Model : Datalogin.php

class Datalogin extends CI_Model {

    function auth_user($username,$password){
        $query=$this->db->query("SELECT * FROM user WHERE username='$username' AND password=md5('$password') LIMIT 1");
        return $query;
    }

    function data_user(){
        return $query=$this->db->query("SELECT * FROM user WHERE id='" .$_SESSION['id']."'");   
    }
}

if I filter based on a user who logged in, add this script in the controller is also not running.

$access = $this->datalogin->data_user()->row_array(); 
$login = $this->session->set_userdata($access);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.38817 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO