开发者

Check whether user is logged in symfony

I am using symfony 1.4.8 and Propel as ORM. To create a login system I am using sfGuardPlugin. I have configured everything, and the login system is working. But I have a problem in getting the sfGuard user data such as sfGuardUser id, and on login I am getting the back button problem; as I login and press the back button, it again shows the Login Page.

Here is the code that I am using in the view to check whether the user is logged in:

<?php if($sf_user->isAuthenti开发者_开发知识库cated()): ?>
         <?php echo link_to('Logout', '/logout') ?>
<?php else: ?>
         <?php echo link_to('Login', '/login') ?>
<?php endif; ?>

and if I use this method to get sfGuardUser id:

if($this->getUser())
{
    $login_id=$this->getUser();
    $c=new Criteria();
    $c->add(SfGuardUserPeer::USERNAME,$login_id);
    $sf_guard_user=SfGuardUserPeer::doSelectOne($c);
    if($sf_guard_user)
    {
        $sf_id=$sf_guard_user->getId();          

    }
}

it gives me the error:

(Fatal error: Nesting level too deep - recursive dependency? in D:\development\sfprojects\proj\lib\vendor\symfony\lib\plugins\sfPropelPlugin\lib\vendor\propel\util\DebugPDOStatement.php on line 124)


first of all, i dont think that this code has something wrong,

<?php if($sf_user->isAuthenticated()): ?>
         <?php echo link_to('Logout', '/logout') ?>
<?php else: ?>
         <?php echo link_to('Login', '/login') ?>
<?php endif; ?>

I generally use something like:

  <div id="left_section">
      <!-- This section is devoted to user context op -->
      <?php if(!$sf_user->isAuthenticated()):?>
        <div id="log_form">
          <?php echo include_partial('user/login',array('form' => new sfGuardFormSignin()))?>
        </div>
        Dont have an account yet?
        <?php echo link_to('Register now!','user/new')?>
      <?php else:?>
        <div id="user_info">
         <?php echo include_partial('user/info')?>
       </div>
      <?php endif?>
    </div>

I think the Browser back button is a pain in the ars for web developers, so if that is what your saying about "the back button" that completly discouraged. Try redirecting the user when he's logged in to a loggedin page or use a slot to render the welcom message/login form , so that way you ensure he/she always sees what they should. Also, redirecting will make the server render everything again, and ensure the session information is complete.

About the method to get the User id, it should look like something like this, lets suppose that your are in an action:

$this->getUser()->getGuardUser()->getId()

and that should do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜