简体   繁体   English

如何在Joomla中检索用户ID

[英]How to retrieve the user id in Joomla

I am doing coding in Joomla and now i want to retrieve the User id that were Log in. When the user got logged in then the it works fine but when it clicks on my page then it shows an error. 我正在Joomla中进行编码,现在我想检索已登录的用户ID。当用户登录时,它可以正常工作,但是当它单击我的页面时,它显示错误。 I think their may be caching or session problem. 我认为他们可能是缓存或会话问题。 is it? 是吗? Here is my Joomla page code: 这是我的Joomla页面代码:

  <?php
  define( '_JEXEC', 1 );
  define( 'DS', DIRECTORY_SEPARATOR );
  define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
  defined('_JEXEC') or die;
  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' );
  $mainframe =& JFactory::getApplication('site');
  $mainframe->initialise();  
  // Initialise variables.
  $app = JFactory::getApplication();
  $user = JFactory::getUser();
  $userId = $user->get('id');
  $dispatcher = JDispatcher::getInstance();
  $userId = $user->get('id');
  echo $userId;
  ?>

Now in this code it retrieves 00 as a user id. 现在,在此代码中,它将检索00作为用户ID。 But the user is logged in. So what is the problem? 但是用户已登录。那是什么问题?

使用此代码获取登录的详细用户

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

这行代码将获取当前登录的用户ID。

$user_id = JFactory::getUser()->id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM