Question

I've created a php page, but the content may only be available for a particular group Joomla.

The file is within a folder, and this folder is in the root of the site with joomla!

How do I get my php page, check the user data: UserGroupID

/ root
/ root / administrator
/ root / components
...
/ root / folder
/ root / folder / file.php

And file.php

$content_to_group_id = 7;

if ($group_id_user == $content_to_group_id) {
// show
} else {
// error
}
Was it helpful?

Solution

Try this,

Load joomla frame work to your page with following codes.

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
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();

$user = JFactory :: getUser();//if you want to get current users details then empty params other wise pass like JFactory::getUser($user_id);
echo "<pre/>";
print_r($user)//This resulted array have the user group of the user.

Hope its help you..

OTHER TIPS

Probably the easiest way is to utilize the Joomla framework to get the user's id, query the database for a matching record in the #__user_usergroup_map table. The code below hasn't been tested and will require you to load the Joomla framework into your script.

$user =& JFactory::getUser();
$userID = $user->get('id');

// allowed group_id
$groupID = 7;

$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('user_id');
$query->from('#__user_usergroup_map');
$where->where('user_id = ' . $userID . 'and group_id = ' . $groupID); 
$db->setQuery($query);
$db->Query();
$num_rows=$db->getNumRows();

if ($num_rows === 1) {
  // only one matching record 
}
else {
  // Sorry, you aren't allowed here.
}

Note you are using Joomla! 3+, so some of the code above my result in errors due to deprecated methods and or PHP version. Below you find some useful lines that may help you,especially the $groups variable, which returns an array of the authorized user groups. Then you just have to check your user id against the array $groups.

$user = JFactory::getUser();
$db = JFactory:: getDbo();
$groups = $user->getAuthorisedGroups();
$input = JFactory::getApplication()->input;

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top