The component com_users handles the tasks of adding users, login/logout, activating/deactivating of users, pasword reset tasks. We would like to show you how to add a Joomla user from external page.
There are two ways you can add users externally:
- Create an html form with correct fields and submit it the correct Joomla url so that Joomla handles the registration, or
- If you want to do it in your custom method, you need to create both the html form and the and the script that will add the user to the Joomla.
The regular way
You create an html form that contains the correct field names and place it in the joomla root. The field names should match with that of the general joomla registration form.
Viewing the source of a Joomla registration form we can see that the form has text fields named name, username, Email, password, password2 and hidden fields task, id, gid. There is another hidden field and it's name is generated by JUtility::getToken() and the value of the field is 1. This field value is checked at the time of submitting the registration information. We will talk about this later. On submitting the registration form the registration information is processed by the register_save method of the UserController in com_user, which has been specified by the hidden field named task in the registration form.
Now we talk about the UserController in the com_user. In the register_save method there is a line which checks the token value from the submitted form. As we are using a static html form we either need to generate the token name on the html form or if we don’t want to include the token in the form then we need to bypass the line of code which checks for the token.
JRequest::checkToken() or jexit( 'Invalid Token' );
In the register_save method of the UserController in the com_user. Since we will be using a html form commonly so we go for the second option and comment the line for checking token
// JRequest::checkToken() or jexit( 'Invalid Token' );
The submitted registration information is then processed by the register_save method. The data is actually saved in the save method of the UserController.
Here is the other way
In this approach we use the previously created form and submit it to the custom script that we create. This approach is basically to copy the functionality of the register_save() method in the UserController in com_user to an external script and submit the html registration form to that script.
Let us take a look at the script: To access the joomla environment in an external script, the following snippet is added to the start of the script:
define( '_JEXEC', 1 ); define('JPATH_BASE', dirname(__FILE__) );// this is when we are in the root define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); $mainframe =& JFactory::getApplication('site'); $mainframe->initialise();
This snippet basically makes available the joomla framework available in the script. The code snippet is found in the index.php of the joomla root.
Now we import the functionality of the register_save() method in the script.
Step 1. Check for request forgeries, we comment this out since tokens are not generated in the html page
//JRequest::checkToken() or jexit( 'Invalid Token' );
Step 2. Get required system objects
$user = clone(JFactory::getUser()); $pathway =& $mainframe->getPathway(); $config =& JFactory::getConfig(); $authorize =& JFactory::getACL(); $document =& JFactory::getDocument();
Step 3. If user registration is not allowed, show 403 not authorized(Not needed)
$usersConfig =& JComponentHelper::getParams( 'com_users' ); if ($usersConfig->get('allowUserRegistration') == '0') { JError::raiseError( 403, JText::_( 'Access Forbidden' )); return; }
Step 4. Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' ); if (!$newUsertype) { $newUsertype = 'Registered'; }
Step 5. Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' )) { JError::raiseError( 500, $user->getError()); }
Step 6. Set some initial user values
$user->set('id', 0); $user->set('usertype', ''); $user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' )); $date =& JFactory::getDate(); $user->set('registerDate', $date->toMySQL());
Step 7. If user activation is turned on, we need to set the activation information(Not needed)
$useractivation = $usersConfig->get( 'useractivation' ); if ($useractivation == '1') { jimport('joomla.user.helper'); $user->set('activation', md5( JUserHelper::genRandomPassword()) ); $user->set('block', '1'); }
Step 8. Save the details of the user
$user->save();
After this we can use the php header function to redirect the user to the desired location.