【问题标题】:Symfony 1.4 ghost user - current user changes to different userSymfony 1.4 ghost user - 当前用户更改为不同用户
【发布时间】:2012-09-17 12:45:43
【问题描述】:

我有以下代码在我的应用程序中显示当前登录的用户。奇怪的是,当用户从一个页面浏览到另一个页面时,登录用户的用户名会偶尔更改为数据库中的另一个用户。注销并重新登录然后显示当前用户名。

 <? if ($sf_user->isAuthenticated()){?>
  <div id="welcome">
   You are signed in as <strong><?php echo sfContext::getInstance()->getUser()->
   getGuardUser()->getName()?></strong> <a href="/logout" class="button">Logout</a>
  </div>
  <div class="clear"></div>
 <? } ?>

任何想法可能导致的事情?

我可以确认两个用户在同一天登录,但在不同的位置。

【问题讨论】:

    标签: symfony1 symfony-1.4 sfguard


    【解决方案1】:

    在您的视图中使用$sf_user 而不是sfContext

    喜欢:

    <? if ($sf_user->isAuthenticated()){?>
           <div id="welcome">
                  You are signed in as <strong><?php echo $sf_user->getName()?></strong> <a href="/logout" class="button">Logout</a>
            </div>
            <div class="clear"></div>
    <? } ?>
    

    【讨论】:

    • 感谢您的回复,使用 $sf_user 会阻止问题的发生,如果是,为什么?
    • sfContext 是整个应用程序对象,sfUser(视图中的 $sf_user)被引用到当前用户会话。
    • @Wilq 必读:Why sfContext::getInstance() Is Bad,如果这个答案解决了你的问题,别忘了accept it
    • 使用$sf_user 是最佳实践,但为什么要解决问题?查看sfContext 类可以看到filterTemplateParameters 函数从$this-&gt;factories['user'] 获取$sf_user 参数,与getUser 函数完全一样。
    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多