Rectangle 27 1

$this->session->set_userdata('edit', $values); is it really storing in ci_session table? make sure there is no errors with ci_session

Muhit, yes everything works 100% except the sessions

php - Codeigniter says session userdata blank, but exists in db - Stac...

php codeigniter session
Rectangle 27 2

View variables are separate from other variables in CI. I think (am still a bit of a CI newbie) that $this->session is not available in Views. This CI forum post discusses a couple of possible solutions to the problem http://codeigniter.com/forums/viewthread/100587/#508098 - one is to pass the value of 'userdata' from the session into the $data array you pass to the view - which seems the best solution.

I've actually tried that already, same result :/

Can you call $this->session->all_userdata() and print the array (maybe using error_log()) you get back - just before loading the view - to see what's in the userdata?

OK, just a wild guess but it sounds to me as if when CI goes to retrieve the session from the DB, it's not finding a match. I see in your config file that you have set sess_match_ip to TRUE, when the default is FALSE. What happens when you set it back to true. It really sounds like a matching problem, so if that doesn't work, I'd also experiment with setting sess_match_useragent to FALSE too.

i set 'sess_match_useragent' to false and it worked! but how what i people are on the same ip address, its a bit less secure due to no useragent check

php - Codeigniter says session userdata blank, but exists in db - Stac...

php codeigniter session
Rectangle 27 1

If its set to true, cookies will only be set if a secure connection exists i.e. when your website is running on HTTPS.

CodeIgniter setting userdata session - Stack Overflow

codeigniter session
Rectangle 27 4

Regarding retrieving values from the session

userdata() is a method. The way you are accessing it treats it as an array:

$this->session->userdata['user']['name']; // Bad

As the docs describe, you should pass the key of the value you want to retrieve as a parameter to the userdata method:

$user = $this->session->userdata('user');
// Now you can echo $user['name'];
$user = $this->session->user;
// Now you can echo $user['name'];

Check the timestamp fields. They show 3 different times, meaning those are 3 separate events on the site. The first occurs at 1501911275, then 6 seconds later there are another 2 records, and then 139 seconds later the last record. Those represent 3 separate requests to the site - probably you were visiting different pages, or testing and reloading.

As to why there are multiple records, that's because the session ID is periodically regenerated. Exactly how often that happens depends on your configuration see the sess_time_to_update config option. How often they are destroyed also depends on your config (see sess_regenerate_destroy in the same section of the docs) - but normally older session records stick around for a while and are cleaned up later by garbage collection. So while you have only 1 "current" session record, old records can persist for a while.

From the wording of your question I think you want to use the files session driver, and only tried the database driver for testing. If that's correct, there is a problem in your files session configuration which you should fix.

only absolute paths are supported for $config['sess_save_path'].

The comments in config/config.php also say:

Your configuration specifies that session data should be saved in ci_sessions, which is not an absolute path:

$config['sess_save_path'] = 'ci_sessions'; // Bad

I am not sure how will be interpreted, but my guess is the directory won't exist, and the web server will not have permissions to create it. I can't imagine why it works on your live server.

Change the path to an absolute path, and make sure your web server can write to it. Again from the docs

mkdir /<path to your application directory>/sessions/
chmod 0700 /<path to your application directory>/sessions/
chown www-data /<path to your application directory>/sessions/

(change www-data to the user your web server is running as). And then:

$config['sess_save_path'] = '/<path to your application directory>/sessions/';

No, I tried everything what all suggested but nothing was working. Finally, I found the issue, CI 3.1.6 was not supporting PHP 7.1, when I moved back to 5.6, it started saving the session. Thank you so much for your help.

@ShanilSoni I just set up a Docker container with PHP 7.0 and a fresh install of Codeigniter 3.1.6, and sessions work fine. I don't think it is a PHP version problem.

@ShanilSoni If you tried everything suggested here you would have seen that neither of the other 2 answers here actually work, right?

That's why I mentioned in my own answer that maybe this is not the correct explanation but that worked somehow. Yes, none of the answers mentioned here did work. This error I have been trying to resolve past 15 days and for the pages that required sessions to be read, I commented out that code to have that worked otherwise.

php - Sessions not working in localhost but work in live server (Codei...

php codeigniter session
Rectangle 27 3

$newdata = array('email' => $email, 'is_logged_in' => true);
$this->session->set_userdata($newdata);
$email = $this->session->userdata('email');
$is_logged_in = $this->session->userdata('is_logged_in');
$dataArray = $this->session->userdata();

thank you as I said I will use userdata() instead of magic getter, if they are equal, i think userdata(); is more readable then just $this->session->item

php - session on CodeIgniter 3, userdata or not? - Stack Overflow

php codeigniter session
Rectangle 27 3

$newdata = array('email' => $email, 'is_logged_in' => true);
$this->session->set_userdata($newdata);
$email = $this->session->userdata('email');
$is_logged_in = $this->session->userdata('is_logged_in');
$dataArray = $this->session->userdata();

thank you as I said I will use userdata() instead of magic getter, if they are equal, i think userdata(); is more readable then just $this->session->item

php - session on CodeIgniter 3, userdata or not? - Stack Overflow

php codeigniter session
Rectangle 27 3

Sometimes CI cannot read its own cookie file, and keeps on changing session ID. This error is practically impossible to reproduce. Me, personally have seen it only ones. i tried following steps.

Used DB for CI session.
Made sure that 4k space is not exceed.
Clearing browsers cookies manually.
Examining PHP session cookie name and I fixed the problem by renaming CI session cookie.

Thanks @joe i am already using db for session and also done your suggested steps but still same

@Rakesh Sharma . The characters limit for user_agent coloumn in DB is 120 or more ? have u checked it ?

php - Session userdata not updating on server codeigniter - Stack Over...

php codeigniter session
Rectangle 27 2

In Codignitor 3 you can get values from $this->session obejct as:

//Normal PHP    
$name = $_SESSION['name'];    

// IN Ci:    
$name = $this->session->name    

// you can also get by
$name = $this->session->userdata('name');

And whats wrong with your code?

You are using session object as

$this->session->data

This should be

$this->session->userdata

For this, yes it's better to use framework libaray becuase you can manage and reuse your code easily.

With regard to userdata() vs $_SESSION[]: The main advantage of using $this->session->userdata('item'); is that the function takes care of checking that 'item' is a valid key. The same is true for the magic getters (ie $item = $this->session->item;). All of these functions, and many others in the sessions library are working directly with the $_SESSION array. So there is nothing wrong with using $_SESSION directly.

thank you I will you userdata() then i think is more readable than use just the magic getter...

php - session on CodeIgniter 3, userdata or not? - Stack Overflow

php codeigniter session
Rectangle 27 2

In Codignitor 3 you can get values from $this->session obejct as:

//Normal PHP    
$name = $_SESSION['name'];    

// IN Ci:    
$name = $this->session->name    

// you can also get by
$name = $this->session->userdata('name');

And whats wrong with your code?

You are using session object as

$this->session->data

This should be

$this->session->userdata

For this, yes it's better to use framework libaray becuase you can manage and reuse your code easily.

With regard to userdata() vs $_SESSION[]: The main advantage of using $this->session->userdata('item'); is that the function takes care of checking that 'item' is a valid key. The same is true for the magic getters (ie $item = $this->session->item;). All of these functions, and many others in the sessions library are working directly with the $_SESSION array. So there is nothing wrong with using $_SESSION directly.

thank you I will you userdata() then i think is more readable than use just the magic getter...

php - session on CodeIgniter 3, userdata or not? - Stack Overflow

php codeigniter session
Rectangle 27 1

I tried the same test on CodeIgniter 2.0.0 and it worked fine - the data types stored in the session with set_userdata() were preserved when reading back session data with userdata(). So this seems to be a bug with CI 1.7.3 when storing session data, then doing a redirect, then reading the session data.

codeigniter set_userdata not preserving types in session data - Stack ...

session codeigniter user-data
Rectangle 27 1

I tried the same test on CodeIgniter 2.0.0 and it worked fine - the data types stored in the session with set_userdata() were preserved when reading back session data with userdata(). So this seems to be a bug with CI 1.7.3 when storing session data, then doing a redirect, then reading the session data.

codeigniter set_userdata not preserving types in session data - Stack ...

session codeigniter user-data
Rectangle 27 1

$query = $this->db->select('user_data')->get('ci_sessions');

$user = array(); /* array to store the user data we fetch */

foreach ($query->result() as $row)
{
    $udata = unserialize($row->user_data);

    /* put data in array using username as key */
    $user[$udata['user_name']] = $udata['user_role']; 
}

from the result array, u can fetch needed information.

That's identical to the solution I listed and said I was trying to avoid if possible. I realise it may be necessary but only as a last resort.

php - Codeigniter Session - Access Userdata value for all users from D...

php codeigniter session
Rectangle 27 1

I found the answer to my question. In my model I was adding all data of the logged in user. One of the columns contain the IP Address. And I don't know why exactly but when I add the IP Address to the session it removes the session, and adds a new session with the default codeigniter session data and a new session id.

php - Codeigniter userdata removed right after creation - Stack Overfl...

php codeigniter session user-data
Rectangle 27 1

I found the answer to my question. In my model I was adding all data of the logged in user. One of the columns contain the IP Address. And I don't know why exactly but when I add the IP Address to the session it removes the session, and adds a new session with the default codeigniter session data and a new session id.

php - Codeigniter userdata removed right after creation - Stack Overfl...

php codeigniter session user-data
Rectangle 27 3

all_userdata()

You can write that within your Controller and send the returned value to the view, as follows:

$data['userdata'] = $this->session->all_userdata();
$this->load->view('your/view.php', $data); // use $userdata in the view

Note: Using the session library directly inside the views may cause unexpected problems. Try managing your sessions in the Controller instead of the View.

Followed your advice, the problem gets stranger. Now when my view loads, var_dump($this->session->all_userdata()); results in completely blank page. Error reporting is turned on in Codeigniter, and there is no exit statement following the var_dump. As in I stated in my post above, this problem only exists on the web server. My development machine just shows me an array of useful information. Any other ideas?

php - Codeigniter Session->userdata is null - Stack Overflow

php codeigniter session
Rectangle 27 1

To solve this problem, you could create a token string with a unique key and store the pairs of keys/tokens in the session (as userdata in CodeIgniter).

  • Creating a unique token key.
  • Storing the key/token pair in the session (userdata).
<input>
  • Validating the posted key/token by checking the existence of key/token in the session.
$csrf_key   = "TOKEN_" . mt_rand(0, mt_getrandmax());
$csrf_token = hash("sha512", mt_rand(0, mt_getrandmax()));
// Store the key/token pair in session
$this->session->set_userdata($csrf_key, $csrf_token);
input
<form action="path/to/handler/page" method="post">
    <!-- form input elements -->
    <input type="hidden" name="csrf_key" value="<?php echo $csrf_key; ?>">
    <input type="hidden" name="csrf_token" value="<?php echo $this->session->userdata($csrf_key); ?>">
</form>
if (count($_POST)) {
    if (! isset($_POST['csrf_key']) or ! isset($_POST['csrf_token'])) {
        die('No CSRF token found, invalid request.');     
    }

    $key   = $this->input->post('csrf_key');
    $token = $this->input->post('csrf_token');

    if ($token !== $this->session->userdata($key)) {                
        die('Invalid CSRF token, access denied.');
    }
}

php - One token vs. multiple tokens to prevent CSRF attacks - Stack Ov...

php codeigniter security csrf
Rectangle 27 2

$this->session->set_userdata('logged_in', $sess_array);
$this->session->set_userdata($sess_array);
$this->session->userdata('id');
$this->session->userdata('username');

I completely missed this! Either this or my answer is probably the solution to the OP's problem, +1.

PHP (CodeIgniter) Sessions Empty - Stack Overflow

php codeigniter session session-variables
Rectangle 27 1

$this->session->userdata('user_id')
public function check_logged($user2=""){
  $check_session = $this->session->userdata('user_id');
     if(!$check_session) {
       redirect('social/index');
}

Yes that's true i've updated this portion of code but the problem persist on Safari

I'm also saving the sessions as file. With Chrome and Firefox the session is saved but with Safari the session is empty

@mds does the session flash data works?

Yes it's OK i've just see Chrome is generating 1 file where save session info data, Safari is generating 3, 4 or more then 5 files 1 with all session data and the others are empty..

config.php
sess_match_useragent

php - CodeIgniter 3 Session not working on Safari - Stack Overflow

php codeigniter session safari
Rectangle 27 0

This page say u should change Session library in \system\libraries I Rename default Session file And create new Session file and put the code on it

Codeigniter Session only writes userdata when sess_use_database is FAL...

codeigniter session