Joomla 2.5从外部脚本获取用户数据

Jon*_*nah 0 joomla external-script joomla2.5

我需要从Joomla本身以外的程序中获取当前登录到Joomla的用户的信息.我从1.5升级到2.5,而我以前所做的不再适用了.

<?php

define( '_VALID_MOS', 1 );

include_once( 'globals.php' );
require_once( 'configuration.php' );
require_once( 'includes/joomla.php' );
$option="test";
$mainframe = new mosMainFrame( $database, $option, '.' );
$mainframe->initSession();

$my = $mainframe->getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
Run Code Online (Sandbox Code Playgroud)

经过一番研究,我想出了这个:

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

require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );

$my =& JFactory::getUser();
$joomla_name = $my->name;
$joomla_email = $my->email;
$joomla_password = $my->password;
$joomla_username = $my->username;
Run Code Online (Sandbox Code Playgroud)

它不会产生任何错误,但似乎有效.但是,用户对象为空.此脚本与Joomla安装位于同一目录中.可能是什么问题?谢谢!

资料来源:

http://www.cmsbloke.com/accessing-joomla-objects-from-an-external-script/

小智 5

取自 http://docs.joomla.org/How_to_access_session_variables_set_by_an_external_script

解决方案:session_start();在外部脚本中替换为

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' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Run Code Online (Sandbox Code Playgroud)

请务必更改JPATH_BASE以适合您的目录结构.

$_SESSION[ 'name' ] = "value";你的外部脚本替换

$session =& JFactory::getSession();
$session->set('name', "value");
Now you can retrieve this session variable using:

$session =& JFactory::getSession();
echo $session->get('name');
Run Code Online (Sandbox Code Playgroud)