use*_*413 20 php joomla joomla1.5
我们正在为Joomla创建一个XML API,允许合作伙伴网站在我们的网站上为其用户创建新帐户.
我们已经创建了一个独立的PHP脚本来处理和验证API请求,但现在我们需要实际创建新帐户.我们原本考虑只是进行CURL调用来提交注册表单,但我们意识到用户令牌存在问题.是否有另一种干净的方式来创建一个用户帐户而不进入Joomla的胆量?如果我们确实需要做一些手术,最好的方法是什么?
Gmo*_*onC 17
你应该使用Joomla内部类,比如JUser,因为有很多内部逻辑,比如密码salting.创建一个自定义脚本,该脚本使用API请求中的值,并使用Joomla用户类中的方法将用户保存在数据库中.
使用自定义代码添加joomla用户的两种方法是一个很棒的教程.这种方法有效.我在一些项目中使用过这种方法.
如果您必须在Joomla 之外访问Joomla Framework ,请检查此资源.
mav*_*ros 11
基于waitinforatrain的答案,这对于登录用户来说没有正常工作(如果你在后端使用它实际上很危险),我已经对它进行了一些修改,在这里它完全适合我.请注意,这是针对Joomla 2.5.6,而此主题最初为1.5,因此上面的答案:
function addJoomlaUser($name, $username, $password, $email) {
jimport('joomla.user.helper');
$data = array(
"name"=>$name,
"username"=>$username,
"password"=>$password,
"password2"=>$password,
"email"=>$email,
"block"=>0,
"groups"=>array("1","2")
);
$user = new JUser;
//Write to database
if(!$user->bind($data)) {
throw new Exception("Could not bind data. Error: " . $user->getError());
}
if (!$user->save()) {
throw new Exception("Could not save user. Error: " . $user->getError());
}
return $user->id;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
只需访问文档页面:http: //docs.joomla.org/JUser
还竞争单页样本以注册Joomla中的新用户:
<?php
function register_user ($email, $password){
$firstname = $email; // generate $firstname
$lastname = ''; // generate $lastname
$username = $email; // username is the same as email
/*
I handle this code as if it is a snippet of a method or function!!
First set up some variables/objects */
// get the ACL
$acl =& JFactory::getACL();
/* get the com_user params */
jimport('joomla.application.component.helper'); // include libraries/application/component/helper.php
$usersParams = &JComponentHelper::getParams( 'com_users' ); // load the Params
// "generate" a new JUser Object
$user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded
$data = array(); // array for all user settings
// get the default usertype
$usertype = $usersParams->get( 'new_usertype' );
if (!$usertype) {
$usertype = 'Registered';
}
// set up the "main" user information
//original logic of name creation
//$data['name'] = $firstname.' '.$lastname; // add first- and lastname
$data['name'] = $firstname.$lastname; // add first- and lastname
$data['username'] = $username; // add username
$data['email'] = $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'] = $password; // set the password
$data['password2'] = $password; // confirm the password
$data['sendEmail'] = 1; // should the user receive system mails?
/* Now we can decide, if the user will need an activation */
$useractivation = $usersParams->get( 'useractivation' ); // in this example, we load the config-setting
if ($useractivation == 1) { // yeah we want an activation
jimport('joomla.user.helper'); // include libraries/user/helper.php
$data['block'] = 1; // block the User
$data['activation'] =JUtility::getHash( JUserHelper::genRandomPassword() ); // set activation hash (don't forget to send an activation email)
}
else { // no we need no activation
$data['block'] = 1; // don't block the user
}
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
}
return $user; // else return the new JUser object
}
$email = JRequest::getVar('email');
$password = JRequest::getVar('password');
//echo 'User registration...'.'<br/>';
register_user($email, $password);
//echo '<br/>'.'User registration is completed'.'<br/>';
?>
Run Code Online (Sandbox Code Playgroud)
请注意,注册仅使用电子邮件和密码.
调用样本:localhost/joomla/test-reg-user-php?email=test02@test.com&password=pass或只是使用适当的参数创建简单表单