0

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();

     }

Edited by anmol.raghuvanshi1

2
Contributors
4
Replies
16
Views
2 Years
Discussion Span
Last Post by lps
0

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

0

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'

0

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

1

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'];
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.