How do I get the member id of the current user in my extension when I'm in the session_start hook?
ee()->session->userdata('member_id');
Obviously doesn't work; this gives me a server error.
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It only takes a minute to sign up.
Sign up to join this communityFirst 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;
break;
case 'c' :
$this->sdata['session_id'] = ee()->input->cookie($this->c_session);
break;
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;
break;
}
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) {
//FAILED TO FIND SESSION
return false;
}
Run the query, do checks on the result and...
$memberID = (int) $query->row('member_id');
Finally you have the member ID.