Session 如何将外部脚本限制为以管理员身份登录的用户?

Session 如何将外部脚本限制为以管理员身份登录的用户?,session,joomla,joomla2.5,Session,Joomla,Joomla2.5,我正在创建一个新页面,该页面将从Joomla 2.5中的管理员页面重定向。当我在浏览器中键入URL时,该页面将显示。我需要限制视图,使其仅在管理员登录其帐户时可见。你能帮我吗 这是我的代码: define( '_JEXEC', 1 ); define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPAT

我正在创建一个新页面,该页面将从Joomla 2.5中的管理员页面重定向。当我在浏览器中键入URL时,该页面将显示。我需要限制视图,使其仅在管理员登录其帐户时可见。你能帮我吗

这是我的代码:

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' );

$app = JFactory::getApplication('site');
$user = JFactory::getUser();
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';

//$app->isAdmin() always through null value

print_r($user);

echo $user->username;
当我以站点用户身份从我的站点(成员登录)登录时,我得到以下信息:

       JUser Object
      (
          [isRoot:protected] => 
          [id] => 2
          [name] => name of user
          [username] => username
          [email] => username@gmail.com
          [password] => 695c263968014c89bbf3159aa4:YoBWR6uzmUBMcqfj5hPzCIp7a6maYd
          [password_clear] => 
          [usertype] => 
          [block] => 0
          [sendEmail] => 1
          [registerDate] => 2014-11-24
          [lastvisitDate] => 2014-11-25 15:29:45
          [activation] => 
          [params] => {}
          [groups] => Array
          (
              [2] => 2
          )

          [guest] => 0

在过去的8天里,我一直在忍受这个问题。你能帮我解决这个问题吗?

请确保首先加载Joomla(请参阅Joomla Stackexchange)

更新添加了完整的脚本

将此代码保存在Joomla文件夹根目录中的php文件中。它将工作,您将看到输出。从那里,你可以调整它的位置等工作与任何你试图完成

if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
$app = JFactory::getApplication('site');
$user = JFactory::getUser();
$groups = $user->groups;

if ($app->isSite()) echo 'Running from Joomla Front End site<Br/>';
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';
if($user->id) {
    echo $user->username.' is logged in<Br/>';
    if (isset($groups[8])) echo " - User is a Super User <Br/>";
    if (isset($groups[7])) echo " - User is an Administrator <Br/>";
    if (isset($groups[6])) echo " - User is an Manager <Br/>";
}else{
    echo 'Not logged in<Br/>';
}

if(!isset($groups[7])) die("You must be an administrator to run this");

刚刚用一个功能齐全的脚本更新了答案,该脚本从joomla站点的根目录开始工作。您现在已经拥有了所需的一切。isSite()已设置,但即使管理员已登录,isAdmin也未设置。。。您能告诉我如何检查管理员是否登录了吗?isSite/isAdmin是关于用户是登录到站点的前端还是后端。我添加了如何确定用户所在的用户组。表#uu usergroups包含这些组,因此您可以检查您想要的任何类型的用户组。能否指定必须放置此代码的确切文件或文件夹位置。这将对我很有帮助。它不是由扩展创建的,而是核心php页面。在管理员文件夹内。我需要限制非管理员(超级管理员)访问此页面。。。你能帮我做这个吗。。提前感谢…这段特定的代码必须位于joomla站点的根目录中,这样它才能找到这些文件,但经过调整后,我已经从多个位置运行了相同的代码。请澄清问题…此脚本是由登录管理员执行的,还是应该“执行某些操作”是否有管理员登录?如果是这样的话,他们是登录到主站点还是登录到管理站点有关系吗?我正在为管理员创建一个报告页面。我只需要检查是否管理员登录或不从管理员登录。我刚把这个文件放进管理员文件夹。是这样吗?或者我必须将此页面写入管理员文件夹中的任何其他位置..?我添加了一个变体以在管理员中使用-从那里运行时,您必须以“管理员”而不是“站点”的身份获取$app
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
$app = JFactory::getApplication('site');
$user = JFactory::getUser();
$groups = $user->groups;

if ($app->isSite()) echo 'Running from Joomla Front End site<Br/>';
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';
if($user->id) {
    echo $user->username.' is logged in<Br/>';
    if (isset($groups[8])) echo " - User is a Super User <Br/>";
    if (isset($groups[7])) echo " - User is an Administrator <Br/>";
    if (isset($groups[6])) echo " - User is an Manager <Br/>";
}else{
    echo 'Not logged in<Br/>';
}

if(!isset($groups[7])) die("You must be an administrator to run this");
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}

//$app = JFactory::getApplication('site');
$app = JFactory::getApplication('administrator');
if ($app->isSite()) echo 'Running from Joomla Front End site<Br/>';
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';

$user = JFactory::getUser();
if($user->id) {
    echo $user->username.' is logged in<Br/>';
    $groups = $user->groups;
    if(isset($groups[8])) {
        die("You are a Super User - only Administrators can run this");
        // Do your superuser coding here
    }elseif(isset($groups[7])) {
        die("You are an administrator - you can run this");
        // Do your admin coding here
    }
}else{
    echo 'Not logged in<Br/>';
}