【问题标题】:Create PHP page restricted to groups Joomla创建仅限于组 Joomla 的 PHP 页面
【发布时间】:2013-08-22 16:35:49
【问题描述】:

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

文件在一个文件夹中,这个文件夹在joomla网站的根目录下!

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

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

还有文件.php

$content_to_group_id = 7;

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

【问题讨论】:

标签: php joomla joomla3.1


【解决方案1】:

试试这个,

使用以下代码将 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.

希望对你有所帮助..

【讨论】:

    【解决方案2】:

    可能最简单的方法是利用 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.
    }
    

    【讨论】:

    • 你的问题也是正确的,我会说这是解决方案的第二部分!但只有一个问题可以标记为正确!
    【解决方案3】:

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

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

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多