Member Avatar for anmol.raghuvanshi1

hello,everyone i am trying to access user id from database in session data but no success till now??

//Controller
function upload()
    {
        $user_id = $this->session->userdata('user_id');
        //set preferences
        $config['remove_spaces']=TRUE;
        $config['encrypt_name'] = TRUE; // for encrypting the name
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'jpg|png|gif';
        $config['max_size']    = '10248';

        //load upload class library
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('filename'))
        {
            // case - failure
            $upload_error = array('error' => $this->upload->display_errors());
            $this->load->view('edit_profile', $upload_error);
        }
        else
        {
            // case - success
            $upload_data = $this->upload->data();
            $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
            $this->load->view('edit_profile', $data);

        }
    }




//view
<div class="row">
                    <div class="col-md-12">
                        <input type="file" name="filename" size="20" />
                        <span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
                    </div>
                    <h1>USER ID</h1>
                    <?php print_r($this->session->userdata()) ;?>

what i am really trying to do is according to user_id i want to upload image in database??

<?php print_r($this->session->userdata()) ;?>
results in   

rray ( [__ci_last_regenerate] => 1438785743 [sessiondata] => Array ( [user_id] => 26 [first_name] => aman [last_name] => [username] => aman123 [sex] => [dob] => 0000-00-00 [phone] => 0 [email] => aman@gmail.com [visited_country] => 0 [about_me] => [help_others] => [fav_activity] => [bed_offer] => 0 [couch_country] => 0 [couch_state] => 0 [couch_city] => 0 [couch_addline1] => [couch_addline2] => [profile_picture] => [picture1] => [picture2] => [picture3] => [picture4] => [picture5] => [country] => [state] => [city] => [addline1] => [addline2] => [zip] => 0 [created_on] => 0000-00-00 [password] => 123123 [modified_on] => 0000-00-00 [active] => [user_type] => 0 [ip] => ::1 ) [username] => aman123 [is_login] => 1 ) something like this 

<?php print_r($this->session->userdata('username')) ;?>  prints correct username

but when i do this <?php print_r($this->session->userdata('user_id')) ;?>  nothing is printed

this is how i have created session

My Login Controller
//validation succeeds
                   if ($this->input->post('submit') == "Login")
                   {
                        //check if username and password is correct
                        $usr_result = $this->login_model->get_user($username, $password);
                        if ($usr_result > 0) //active user record is present
                        {
                             //set the session variables
                             $sessiondata = array(
                                  'username' => $username,
                                  //'password'=>$password,
                                  'is_login' => TRUE
                             );
                             //$this->login_model->set_session($username);
                             $this->session->set_userdata($sessiondata);
                            // print_r($this->session->all_userdata());  //to check
                             redirect(base_url("Site/member_area"));
                            // return  $username;

My login Model

My login model
//get the username & password from tbl_usrs
     public function get_user($usr, $pwd)
     {    
          $sql = "select * from tbl_usrs where username = '" . $usr . "' and password = '" .$pwd . "' ";
          $query = $this->db->query($sql);
          $this->session->set_userdata('sessiondata', $query->row_array());
          return $query->num_rows();

     }

Recommended Answers

All 4 Replies

Member Avatar for anmol.raghuvanshi1

There is something which i can't able to figure out plz help me with this

You should gone throught your own array of $this->session->userdata() using <pre><?php print_r($this->session->userdata()) ;?></pre>.
You $this->session->userdata('username') working as it is declared directly beneath $this->session->userdata(). But, your 'user_id' is under 'sessiondata'

Member Avatar for anmol.raghuvanshi1

I canĀ“t understand your answer can you point where I am wrong I have ben waiting for reply whole night..:)

From your code and arrays printed, your user_id should not be $user_id = $this->session->userdata('user_id');. Instead,

$session_data = $this->session->userdata('sessiondata');
$user_id = $session_data['user_id'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.