Joomla 3.0 Register user with php script

2019-05-20 14:16发布

I've searched and found a way to do this with JUser but when I try the script it says an include file can't be found and it doesn't exist on the server. I don't know if this is different for Joomla 3.0 so I was asking for help. Here's the script I tried:

<?php
   define( '_JEXEC', 1 );
   define('JPATH_BASE', dirname(__FILE__) );
   define( 'DS', DIRECTORY_SEPARATOR );
   /* Required Files */
   require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
   require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

   $app = JFactory::getApplication('site');
   $app->initialise();
   require_once JPATH_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
   require_once JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'application'.DS.'component'.DS.'helper.php';
   $model = new UsersModelRegistration();
   jimport('joomla.mail.helper');
   jimport('joomla.user.helper');

   $username = 'jimporttest';
   $name = 'J Port2';
   $email = 'test @ mail.com';
   $password = 'test';
   $data = array( 'username' => $username,
             'name' => $name,
             'email1' => $email,
             'password1' => $password, // First password field
             'password2' => $password, // Confirm password field
             'block' => 0 );
   echo $model->register($data);
?>

标签: joomla
4条回答
淡お忘
2楼-- · 2019-05-20 14:30

Unless your file is in the root folder where the Joomla system is installed, this error is definitely expected.

dirname(__FILE__)

returns the working files path. Therefore you may need to modify the path accordingly. Use

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
  echo JPATH_BASE;

to make sure the 'include' folder in the same path as JPATH_BASE retuns. For instance, if your file is in the 'www/example/test' folder, then use

define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));

to get the correct base path.

Also I found some errors in your codes. Use this working codes for the testing purposes.

            <?php
                define( '_JEXEC', 1 );
                define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));
                define( 'DS', DIRECTORY_SEPARATOR );

                require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
                require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

                $app = JFactory::getApplication('site');
                $app->initialise();   

                require_once  JPATH_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
                $model = new UsersModelRegistration();
                jimport('joomla.mail.helper');
                jimport('joomla.user.helper');


                $username = 'ben';
                $name = 'ben';
                $email = 'test@mail.com';
                $password = 'test';
                $data = array( 'username' => $username,
                         'name' => $name,
                         'email1' => $email,
                         'password1' => $password, // First password field
                         'password2' => $password, // Confirm password field
                         'block' => 0 );

                $return = $model->register($data);
            ?>

'../../' part of the

define( 'JPATH_BASE', realpath(dirname(__FILE__).'../../'));

depends on the deviation of root folder as I explained earlier.

查看更多
欢心
3楼-- · 2019-05-20 14:33

here is the code you can use.

$data['name'] = $fname . ' ' . $lname;
$data['username'] = $email;
$data['email1'] = $email;
$data['email2'] = $email;
$pwd = substr(md5($email . $fname . time()), 0, 6);
$data['password1'] = $pwd;
$data['password2'] = $pwd;

JFactory::getLanguage()->load('com_users');
JModel::addIncludePath(JPATH_ROOT . '/components/com_users/models');

$model = JModel::getInstance('Registration', 'UsersModel');
$return = $model->register($data);
查看更多
Root(大扎)
4楼-- · 2019-05-20 14:39

you have to use the JPATH_BASE in the require_once clause (you are using JPATH_BASE_ROOT which is not defined). Also in JOOMLA 3.0 the helper.php it is not in that position.

Try this code:

<?php
         define( '_JEXEC', 1 );
         define('JPATH_BASE', dirname(__FILE__) );
         define( 'DS', DIRECTORY_SEPARATOR );
         /* Required Files */
         require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
         require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

         $app = JFactory::getApplication('site');
         $app->initialise();
         require_once(JPATH_BASE.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
        //not necessary
        //require_once(JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'application'.DS.'component'.DS.'helper.php';
         $model = new UsersModelRegistration();
         jimport('joomla.mail.helper');
         jimport('joomla.user.helper');

         $username = 'jimporttest';
         $name = 'J Port2';
         $email = 'test @ mail.com';
         $password = 'test';
         $data = array( 'username' => $username,
         'name' => $name,
         'email1' => $email,
         'password1' => $password, // First password field
         'password2' => $password, // Confirm password field
         'block' => 0 );
          echo $model->register($data);
  ?>

I've tried that in my joomla 3.0 installation and it works.

Andrea

查看更多
一夜七次
5楼-- · 2019-05-20 14:42

For >=Joomla 3 we need to do use the following code instead. Changed from "JModel" to "JModelLegacy". Click here to view in detail.

$data['name'] = $fname . ' ' . $lname;
$data['username'] = $email;
$data['email1'] = $email;
$data['email2'] = $email;
$pwd = substr(md5($email . $fname . time()), 0, 6);
$data['password1'] = $pwd;
$data['password2'] = $pwd;

JFactory::getLanguage()->load('com_users');
JModelLegacy::addIncludePath(JPATH_ROOT . '/components/com_users/models');

$model = JModelLegacy::getInstance('Registration', 'UsersModel');
$return = $model->register($data);
查看更多
登录 后发表回答