How do I get the member id of the current user in my extension when I'm in the session_start hook?


Obviously doesn't work; this gives me a server error.

1 Answer 1


First initial thought is: can you use sessions_end hook instead?

Looking at the Session.php code for where the session_start is fired, this hook kicks in before the user data is set from the cookie/session as the next lines that come after the hook are:

// Set the validation type
$this->validation = (REQ == 'CP') ? ee()->config->item('cp_session_type') : ee()->config->item('website_session_type');

Get the session type dependant on what is being accessed - the Control Panel or frontend.

// default to "cookies and sessions" if validation type doesn't exist or is invalid
if ( ! in_array($this->validation, $this->valid_session_types))
    $this->validation = 'cs';

Copying this into your code won't work due to usage of valid_session_types, so you could either replicate the valid types, or just assume 'cs' if the validation variable is not set.

// Grab the session ID and update browser fingerprint based on the validation type
// we use the same URL key whether it's getting the session ID or the browser fingerprint,
// simplifying URI parsing and complicating session hijacking attempts
switch ($this->validation)
    case 's'    :
        $this->sdata['session_id'] = (ee()->input->get('S')) ? ee()->input->get('S') : ee()->uri->session_id;
    case 'c'    :
        $this->sdata['session_id'] = ee()->input->cookie($this->c_session);
    case 'cs'   :
    default     :
        $this->sdata['session_id'] = ee()->input->cookie($this->c_session);
        $this->sdata['fingerprint'] = (ee()->input->get('S')) ? ee()->input->get('S') : ee()->uri->session_id;

The above is the key chunk, where it will get the session_id from the URL or from the cookie.

The EE core Session code then calls the fetch_session_data function which does the DB query to get the member details...

ee()->db->select('member_id, admin_sess, last_activity, fingerprint, sess_start, login_state');
ee()->db->where('session_id', (string) $this->sdata['session_id']);

Main part of the query using the Session ID we got above.

// We already have a fingerprint to compare if they're running cs sessions
// otherwise we'll do it after fetching their member data, presuming the session ID is valid
if ($this->validation == 'cs')
    ee()->db->where('fingerprint', (string) $this->sdata['fingerprint']);

Not too sure if you'll need this, as it refers to fetching it later, but useful if the user has multiple sessions open in different browsers maybe...?

$query = ee()->db->get('sessions');
if ($query->num_rows() == 0 OR $query->row('member_id') == 0) {
    return false;

Run the query, do checks on the result and...

$memberID = (int) $query->row('member_id');

Finally you have the member ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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