繁体   English   中英

创建仅限于Joomla组的PHP页面

[英]Create PHP page restricted to groups Joomla

我已经创建了一个php页面,但是内容可能仅适用于特定的Joomla组。

该文件位于一个文件夹中,并且该文件夹位于joomla!网站的根目录中!

我如何获取我的php页面,请检查用户数据:UserGroupID

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

和file.php

$content_to_group_id = 7;

if ($group_id_user == $content_to_group_id) {
// show
} else {
// error
}

尝试这个,

使用以下代码将joomla框架工作加载到您的页面。

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.

希望它对您有帮助。

可能最简单的方法是利用Joomla框架获取用户的ID,并在数据库中查询#__user_usergroup_map表中的匹配记录。 以下代码未经测试,需要您将Joomla框架加载到脚本中。

$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.
}

请注意,您正在使用Joomla! 3+,由于不推荐使用的方法和/或PHP版本,我上面的某些代码会导致错误。 在下面,您可以找到一些有用的行,尤其是$ groups变量,该变量可以帮助您返回授权用户组的数组。 然后,您只需要对照数组$ groups检查您的用户ID。

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

希望能帮助到你。

暂无
暂无

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

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