0

I need to record some data in the database for a user in the session (a custom add-on) and I'd been using the value returned from PHP's session_id() method, which works well enough.

When the user logs in, I then need to update the data with the logged in member's ID.

However I'm finding that when the user logs in, that session_id() is empty so I lose the link to what was in session for that user prior to login.

I had originally tried using $EE->session->userdata('session_id') but that was zero, which is why I ended up using the native session_id() method.

Does anybody have any suggestions for the best way of achieving this?

Thanks.

1

What about setting a cookie with a random string as the identifier? Will be much more reliable I think.

Just create the string once, then set the cookie and insert your visitor into your database at the same time.

if( ! $cookie = ee()->input->cookie('guest_id') )
{
    ee()->load->helper('string');
    $id = random_string('unique');
    ee()->functions->set_cookie('guest_id', $id);
    // Do your database stuff here.
}
| improve this answer | |
0

I ended up doing this in my module's constructor method:

if (! isset($_SESSION))
{
    session_start();
}

$this->EE->input->set_cookie(array(
    'name'   => 'exp_tp_session_id',
    'value'  => session_id(),
    'expire' => 60*60*24 // 24 hrs
));

And then when a user logs in, I retrieve that cookie value and then use EE's session.

Seems odd that I'd have to do this but it works. Perhaps I'm misunderstanding something...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.