Joomla 3获得用户ID

时间:2015-05-28 18:08:04

标签: joomla

我在myjoomla / myscript /

文件夹中有这个代码
<?php
define('_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']."" ); 
define( 'DS','/' );

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

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

jimport( 'joomla.session.session' );
$session                =& JFactory::getSession();

$user =& JFactory::getUser();
echo "id:".$user->id;

我打印JPATH_BASE就可以了。但没有打印ID,只打印&#34; 0&#34;。我在joomla登录好了。这在localhost中是正常的,但在服务器中没有问题。

由于

1 个答案:

答案 0 :(得分:1)

确实如此,它就像旧代码一样。新代码可能如下所示,

<?php
if (!defined('_JEXEC'))
{
    // Initialize Joomla framework
    define('_JEXEC', 1);
}

// Load system defines
if (file_exists(dirname(__FILE__) . '/defines.php'))
{
    require_once dirname(__FILE__) . '/defines.php';
}
if (!defined('JPATH_BASE'))
{
    define('JPATH_BASE', dirname(__FILE__));
}
if (!defined('_JDEFINES'))
{
    require_once JPATH_BASE . '/includes/defines.php';
}

// Get the framework.
require_once JPATH_BASE . '/includes/framework.php';

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

$user = JFactory::getUser();
echo "id:".$user->id;

注意:在本地和现场测试您的代码它对我有用。但不确定为什么它在您的实时环境中不起作用。但是你的代码确实很旧。