Search code examples
phpjoomla2.5

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. I think their may be caching or session problem. is it? Here is my Joomla page code:

  <?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. But the user is logged in. So what is the problem?


Solution

  • use this code for detail users logged

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