3

Can someone explain to me why I am not capable of forcing a new ExpressionEngine session to be generated for a specific user? I've been following this link: https://stackoverflow.com/questions/13073134/what-is-the-best-way-to-log-a-user-in-programatically-in-expressionengine

What I am trying to do is to temporarily create an EE session, and then to create a channel entry for that logged in user. I can then destroy the session. However, my PHP script seems to fall down when it sees $this->EE. Is there something in particular I need to do to initiate the EE variable to begin with?

See my code below. Do I need to place this code in a template or something else?

// Get the member's id (the member must have permissions to post entries)
$log_in_id = 23; // [email protected] - member id //

// Create session
echo 'before new session | ';
$this->EE->session->create_new_session($log_in_id);
//$this->EE->session->create_new_session(23, TRUE);
echo 'after new session | ';
print_r($this->EE->session);
$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');

$data = array(
    'channel_name'  => 'members', //[Required]
    'title'         => $tmp_firstname.' '.$tmp_lastname, //[Required]
    'author_id'     => $memberID,
    //'entry_id'        => 0,
    'field_id_37'   => $memberID,
    'field_id_38'   => $tmp_firstname,
    'field_id_39'   => $tmp_lastname,
    'field_id_70'   => $tmp_company,
    'field_id_71'   => $tmp_ref,
    'field_id_73'   => $tmp_qty,
    'field_id_74'   => $tmp_remainder,
    'field_id_72'   => $tmp_owner
);
print_r($data);

$this->EE->api_channel_fields->setup_entry_settings(1, $data);

if ($this->EE->api_channel_entries->submit_new_entry(1, $data) === FALSE){ echo '[ERROR]'; }
else{ echo 'SUCCESS';  $this->EE->session->destroy(); }

Any info would be appreciated!

2 Answers 2

7

$this->EE is referring to the EE singleton object that basically powers everything in EE. You need to reference the global object from memory.

$this->EE =& get_instance();

If this doesn't work, then be sure you are executing your code within the EE bootstrap (within a module, plugin, extension, library, or some other approved place).

2
  • thank you for your response. I tried this and it ended up calling a server error. How would I go about calling a plugin when a user has not been authenticated? What is the most suitable approved place? The reason I can not use a pre-authenticated session is that the code I am writing needs to be called directly via a url from a 3rd party integrated application when a particular event has taken place on their side. Jan 8, 2013 at 11:31
  • Cheers Justin, this has now been sorted fully. See below :) Jan 8, 2013 at 13:44
3

This has now been solved thanks to @Justin Kimbrell. I have just placed the original code I was developing inside an ExpressionEngine template and updated its php preferences. Doing so places the code within the bootstrap as suggested above, and still allows for the template to be call via a URL. In the template I am simply just using the same code as above with a single addition: $this->EE->api->instantiate('channel_fields');

2
  • 1
    I was going to mention that you need that library but forgot to include it. Glad you got it working. Jan 8, 2013 at 13:46
  • Yeah I just forgot to paste it into the original question :D Jan 8, 2013 at 14:07

Your Answer

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

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