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

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

Andrea Sha
1#
Andrea Sha Published in 2018-02-14 19:05:27Z

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);
Alex
2#
Alex Reply to 2018-02-14 21:06:00Z

I really don't see how you can "shorten" or otherwise minimize that information. You could offload some things so that you just save, for example, the userid and user level, and then use the userid to get the rest of the information when you need it. But I don't see a problem with storing only 5 strings/ints in session vars if you use them a lot.

You can make the code a little nicer by doing something like this:

$array = array(
    'akses' => $data['level'],
    'session_id' => $data['id'],
    'session_name' => $data['name'],
    'session_username' => $data['username'],
    'session_level' => 'Administrator',
    'session_foto' => $data['foto']
);

$this->session->set_userdata($array);

But it will not change the functionality.

As for your second question (please in the future stick to 1 question per post) I see 3 potential issues:

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

I never see $_SESSION['id'] being set; did you mean session_id here?

Also just return the object without assigning it to a var:

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

Further, I wouldn't store the whole array() like you do $this->datalogin->data_user()->row_array(); Just get it when you need it.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO