Joomla使用rsform注册用户
很多时候,处于某些特殊的原因,我们会使用一些特殊的字段,所以,joomla自带的注册字段不符合要求,这个时候,rsform可以提供我们一个比较好的解决方案
首先在rsform中,创建一个可以使用的类似注册的form,字段包含:name,username,email,password以及一个submit,自动生成的form layout应该类似于
<div class="componentheading">{global:formtitle}</div>
{error}
<table border="0">
<tr class="rsform-block rsform-block-username">
<td>{username:caption}</td>
<td>{username:body}<div class="formClr"></div>{username:validation}</td>
<td>{username:description}</td>
</tr>
<tr class="rsform-block rsform-block-name">
<td>{name:caption}</td>
<td>{name:body}<div class="formClr"></div>{name:validation}</td>
<td>{name:description}</td>
</tr>
<tr class="rsform-block rsform-block-email">
<td>{email:caption}</td>
<td>{email:body}<div class="formClr"></div>{email:validation}</td>
<td>{email:description}</td>
</tr>
<tr class="rsform-block rsform-block-password">
<td>{password:caption}</td>
<td>{password:body}<div class="formClr"></div>{password:validation}</td>
<td>{password:description}</td>
</tr>
<tr class="rsform-block rsform-block-submit">
<td>{Submit:caption}</td>
<td>{Submit:body}<div class="formClr"></div>{Submit:validation}</td>
<td>{Submit:description}</td>
</tr>
</table>
注册的form我们写完了,接下来,我们要把填写的数据传给joomla的数据库,在Script那个tab类面第三个Script called after form has been processed的textarea填写如下code
<?php
// load language
$lang = JFactory::getLanguage();
$lang->load('com_user', JPATH_SITE);
// get config for com_users
$usersConfig = &JComponentHelper::getParams( 'com_users' );
// get the ACL
$acl =& JFactory::getACL();
// "generate" a new JUser Object
// it's important to set the "0" otherwise your admin user information will be loaded
$user = JFactory::getUser(0);
$data = array(); // array for all user settings
// get the default usertype
$usertype = 'Registered';
// set up the "main" user information
$data['name'] = $_POST['form']['name']; // add first- and lastname
$data['username'] = $_POST['form']['username']; // add username
$data['email'] = $_POST['form']['email']; // add email
$data['gid'] = $acl->get_group_id( '', $usertype, 'ARO' ); // generate the gid from the usertype
/* no need to add the usertype, it will be generated automaticaly from the gid */
$data['password'] = $_POST['form']['password']; // set the password
$data['password2'] = $_POST['form']['password']; // confirm the password
$data['sendEmail'] = 1; // should the user receive system mails?
$data['block'] = 0; // don't block the user
// If user activation is turned on, we need to set the activation information
$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
{
jimport('joomla.user.helper');
$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
$data['block'] = 1;
}
if (!$user->bind($data)) { // now bind the data to the JUser Object, if it not works....
JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
return false; // if you're in a method/function return false
}
if (!$user->save()) { // if the user is NOT saved...
JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
return false; // if you're in a method/function return false
}
// Send emails
require_once(JPATH_SITE.DS.'components'.DS.'com_user'.DS.'controller.php');
UserController::_sendMail($user, $data['password']);
?>
这样的话,这个form就可以用来注册用户了。
另外,有些特殊的要求,比如说,注册之后,要自动登录,那加入以下的code
<?php
//auto login
$usersipass['username'] = $_POST['form']['name'];
$usersipass['password'] = $_POST['form']['password'];
$mainframe->login($usersipass);
$mainframe->redirect( 'index.php' );
?>
阅读其他美文