What is the best way to register a user programmatically in ExpressionEngine

I am creating an application that allows the application to remove the ACT URL, which then runs the module method to create a new entry using the ExpressionEngine API. However, since there is no user login / logon, it is not allowed to send a recording to the channel.

What is the best way to do this. Bypass the EE api and send the entry manually or register the user in pro-grammatical mode .. but then how will this work with sessions, etc.?

If the answer is โ€œuser loginโ€, it would be great to see a sample code if possible.

Thanks!

+8
php expressionengine
source share
2 answers

As you noted, there are two ways to add a new entry:

  • manually add database entries
  • use channel APIs (http://expressionengine.com/user_guide/development/api/api_channel_entries.html)

The main differences are that entries added using the API will:

  • Perform all normal data checks (i.e., fields marked as required must not be empty)
  • run any third-party extensions that installed
  • update site statistics (for example, the number of posts by the author)

Adding a record manually is simple enough for simple channels, but it becomes more complicated if you use third-party field types that use additional tables.

To register a participant, you just need to:

// Get the member id (the member must have permissions to post entries) $member_id = 1; // Create session $this->EE->session->create_new_session($member_id); 

use the api channel entries to add the entry, and then:

 // Log user back out $this->EE->session->destroy(); 

when you are done.

+12
source share

You will want to take a look at Auth.php's class. /system/libraries/Auth.php. This class is an abstraction of the authentication API and allows you to do exactly what you want. The loggin as user function uses the same methods as well as a member module.

You can also take a look at the Authenticate module (which is free) for you to understand how others work with the Auth class.

https://objectivehtml.com/authenticate

Here are some pseudo codes to use:

 // Fetch the member from the DB using whatever logic you need $this->EE->db->where('username', 'some-username'); $member = $this->EE->db->get('members'); $this->EE->load->library('auth'); // Load a new Auth_result object which logs in the member $authed = new Auth_result($member->row()); $authed->start_session($cp_session = FALSE); $member->free_result(); 

This code should work, but I did not have time to execute it, so it read the pseudocode.

I should also add that I'm not sure if this is even the best way to solve your problem. I just answer the questions "how to program the user login without knowing their password and without submitting the login form."

+5
source share

All Articles