dtwupu6414 2015-07-07 11:06
浏览 37
已采纳

Joomla 2.5在没有电子邮件地址的PHP脚本中创建用户

I have a a php function that works for creating a new user in Joomla 2.5. This function is used to synchronize an external customer database with Joomla.

The new requirement is that the email address needs to be an optional field. I can't seem to get the JFactory function to work without an email address. Is there another way to get the user created?

function add_joomla_user($username, $email, $name, $password, $group) {
    // Creates a new user in Joomla database with passed in information
    $return_message = '';

    $mainframe =& JFactory::getApplication("site");
    $mainframe->initialise();

    $user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded
    jimport('joomla.application.component.helper');     // include libraries/application/component/helper.php
    $usersParams = &JComponentHelper::getParams( 'com_users' );     // load the Params
    $userdata = array(); // place user data in an array for storing.
    $userdata['username'] = $username;
    $userdata['email'] = $email;
    $userdata['name'] = $name;
    $userdata['password'] = $password;
    $userdata['password2'] = $password;
    $defaultUserGroup = $usersParams->get('new_usertype', $group);
    $userdata['groups']=array($defaultUserGroup);   
    $userdata['block'] = 0; // set this to 0 so the user will be added immediately.

    if (!$user->bind($userdata)) { // bind the data and if it fails raise an error
        JError::raiseWarning('', JText::_( $user->getError())); // something went wrong!!
        $return_message = 'Error binding data: ' . $user->getError();
    }

    if (!$user->save()) { 
        JError::raiseWarning('', JText::_( $user->getError())); 
        $return_message = 'Error creating user: ' . $user->getError();
    } else {
        $return_message = 'Created user';
    }
    return $return_message;
}
  • 写回答

1条回答 默认 最新

  • dtqi87613 2015-07-08 12:55
    关注

    Joomla user handling definitely requires a unique email address for each user. It's tricky and I somewhat hesitate to suggest this, but what you could do if it is missing is substitute a random string or a string generated based on information in your database (like the $userdata['email'] = $username . '@noemail';. That way they will be easy to find later. Of course this means password reset and other functions will never work, but that would be true anyway if the user has no email.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?