douyuefu1372 2013-08-30 15:17
浏览 27
已采纳

如何检查访问者是否已登录,如果是,则设置变量以确定进一步的操作

I am using Joomla 2.5 and in the head of my site I have a module with the following code:

        <?php
$user =JFactory::getUser()->guest;
    if ($user->guest) {
      echo 'Please sign up or sign in to view this'
    }
    else {
      echo 'Here is the content';
    }
       ?>

What I tried but didnt work is to add a variable $member in the first if statement. If the person isnt logged in to show him the echo but also set $member to == true.

Why I want to do this? I think it would make the site load faster if I check the status of $member than to go calling the Joomla API to determine if the user is logged in or not.

The calls to determine if a visitor is a member or not in every page are about 5-7. When I use the JFactory::getUser() function (all of the code above) 5-7 times it does slow down a bit the website. Thats why I thought of creating in the first call the $member == true correct me if I am wrong in thinking this would be better in site perfomance/speed wise.

I tried this:

        <?php
$user =JFactory::getUser()->guest;
    if ($user->guest) {
      echo 'Please sign up or sign in to view this'
      $member == true;
    }
    else {
      echo 'Here is the content';
    }
       ?>

I have the above code in the header module. But when I later in other modules in the same page or in the article if I use a php code it doesn't recognize the status of $member and always determines the user as not logged in.

What am I doing wrong? I'm guessing its something with variables scope, I checked my PHP book but couldn't find the error. Any help appreciated.

  • 写回答

2条回答 默认 最新

  • duanji1056 2013-08-30 15:23
    关注

    You are already calling $user->guest, so try changing:

    $user = JFactory::getUser()->guest;
    

    to this:

    $user = JFactory::getUser();
    

    As an alternative, you could also do use the following:

    $user =& JFactory::getUser();
    if($user->id=0){
        echo 'Please sign up or sign in to view this'
        $member == true;
    }
    else {
        echo 'Here is the content';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式