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.
Now, the Other way:
Here 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.
Now onto 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.
#1. Check for request forgeries, we comment this out since tokens are not generated in the html page
//JRequest::checkToken() or jexit( 'Invalid Token' );
#2. Get required system objects
$user = clone(JFactory::getUser());
$pathway = & $mainframe->getPathway();
$config = & JFactory::getConfig();
$authorize = & JFactory::getACL();
$document = & JFactory::getDocument();
#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;
}
#4. Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype)
{
$newUsertype = 'Registered';
}
#5. Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' ))
{
JError::raiseError( 500, $user->getError());
}
#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());
#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');
}
#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.
That’s it! I hope Joomla developers find this helpful and please feel to post your views and comments to this post.
[ratings] Most popular searches: Technology, Website, AI
Your may interested in our latest blogs
Very good tutorial, thanks!
Excellent tutorial, just what i needed, thanks a heap!!
hi this was not very useful and I wasted several hours trying to implement it, finally got user registrations from the following site. Too bad this site came to the top of google.
Maybe there’s something going on here? I tried what Bryan did and it didn’t work… then I tried this and it worked first try!
Its working like a charm, I have gone through the second way and it is working very perfect in the first instance i mean in the first verification itself.
Thanks alot. My time has saved alot to do this.
Hey Bryan Are you a designer or programmer??? It does have syntax error thats why It might not worked for you.
Hmmmm, dont like the security that is needed by commenting out //JRequest::checkToken() or jexit( ‘Invalid Token’ ); if you do that you are really putting your joomla site at risk.
Is commenting out //JRequest::checkToken() or jexit( ‘Invalid Token’ ) a significant security concern??
i didn’t tried the top method but what brian said is absolutely correct way to create user.
everything is handled by joomla so is best way to create user
hi i can’t understand anybody help me or attach sample forms.I dont know how to give database connection.Plz give me sample forms
I get the error:
Fatal error: Call to a member function bind() on a non-object in /home/…..user.php on line 495
I try to insert the new user from a model and all is ok right up to the line $user->save()
Hello,
How do you sent activation mail after user registered?
I’m trying now to create a new way for user registration in joomla with custom form, but, I have this issue.
Thank you.
All the best.
@Luciano
Hi,
I have faced with the same issue – after registration I don’t have welcome and activation emails.
Did you figure out how to sent email after registration?
Thanks
Informative post, Joomla development empowers you to build up informative websites and competent applications.
wow http://docs.joomla.org/JUser works! Cool Thx!!
Thanks for this. Can you kindly provide similar for v1.6+?
At least i found excellent tutorial, about user register! Thanks a lot!)
I m a newbie, but have a task that requires addition of joomla user from an external script.
Can you elaborate the last statement
“After this we can use the php header function to redirect the user to the desired location.”
Would appreciate if an example of this is provided.
@GC’s Comment RE: redirecting after save.
What you actually want to do is:
redirect(“New User Created”);
?>
Works for joomla 2.5?
it’s big security hole….a bot can register thousand users via CURL in only few seconds!
good info gus, joomla com_user in vulnerability -_-
Why your website with code protected from copying code?
I must retyping all programm code? Author, your are normal?
We have allowed copying of website content now. Hope this scope may not be abused.