Stampa
Categoria: Articles
Visite: 7158
Stella inattivaStella inattivaStella inattivaStella inattivaStella inattiva
 

The following code is useful if you need to know if the current user is logged on Joomla CMS. 

 

The code will print something like the following output

Your name is Quake3 Spirit, your email is spirit AT quakearea.com, and your username is Spirit. Your user group is Super Users

 

You need to set your Joomla Path. In the script was set as "/site"

 

// Mandatory stuff for Joomla...pls customize your joomla path... /site
define( '_JEXEC', 1 );
define('JPATH_BASE',$_SERVER['DOCUMENT_ROOT']. "/site");
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
// Define object for my class joomlaSession
$oJoomla= new joomlaSession();
if ($oJoomla->isLogged()){		
	echo "<p>Your name is {$oJoomla->name}, your email is {$oJoomla->email}, and your username is {$oJoomla->username}. Your user group is  {$oJoomla->usergroup}</p>";
	} else {
	echo "<p>You are not authorized!</p>";
	die();
}
return "";
// Get var user from Joomla Objects
class joomlaSession {
 
        // variable
		public $db;
        public $user;
		public $name;
		public $username;
		public $email;
		public $usergroup;
 		 
		public function __construct() {
			$mainframe = JFactory::getApplication('site');
			$mainframe->initialise();
			$this->db=JFactory::getDBO();
			$this->user  = JFactory::getUser();
			$this->username=$this->user->username;
			$this->name=$this->user->name;
			$this->email=$this->user->email;			
			
			// get user group
			$this->usergroup=$this->getUserGroup();
		}	
 
        // methods
        public function isLogged() {
			if ($user->guest) {
				return false;
			} else {
				return true;
			}
        } 
		
		public function getUserGroup(){
			// Get user group
			
			foreach($this->user->groups as $group){
				$query  = 'SELECT title FROM #__usergroups';
				$query .= ' WHERE id = ' . $group;
				$this->db->setQuery( $query );
				return $this->db->loadResult();
			}
		
		}
}

 

- have fun -

DISQUS - Leave your comments here