【问题标题】:Symfony2 error: failed to start session, already started by PHPSymfony2 错误:无法启动会话,已由 PHP 启动
【发布时间】:2016-04-26 20:50:42
【问题描述】:

在下面的登录脚本中,即使我在验证用户后开始会话,也会抛出 symfony 异常说明:

无法启动会话:已由 PHP 启动。

以下是我的登录功能(我没有使用symfony令牌和安全等。只是会话):

public function loginAction(Request $request){
    if($request->getMethod() == 'POST'){
        $mail = $request->get('umail');
        $pass = $request->get('upass');
        $em = $this->getDoctrine()->getManager();
        $rep = $em->getRepository('SystemBundle:User');

        $user = $rep->findOneBy(array("email"=>$mail,"pass"=>$pass));
        if($user){
            $id = $user->getId();
            $type = $user->getType();
            $session = new Session();
            $session->start();
            $session->set('id',$id);
            $session->set('type',$type);
            if($type == "admin"){
                return $this->redirectToRoute('admin_homepage');
            }
            elseif ($type == "dis"){
                return $this->redirectToRoute('dis_homepage');
            }
            elseif ($type == "comp"){
                return $this->redirectToRoute('company_homepage');
            }
        }
        else{
            return new Response('not found where mail: '.$mail);
        }
    }
}

代码有问题吗? 请帮忙..

【问题讨论】:

  • 你不应该开始一个新的会话,因为 Symfony 会为你处理。您可以使用$session = $request->getSession(); - docs 获取它
  • 成功了,谢谢:)

标签: php symfony session


【解决方案1】:

正如@JimL 所说,

你不应该开始一个新的会话,因为 Symfony 会为你处理。你 可以使用 $session = $request->getSession();

来获取它

这很重要,例如,如果您使用了一个使用会话的侦听器,然后您想使用 Symfony 表单(间接检查会话是否已启动)。

【讨论】:

    猜你喜欢
    • 2015-01-02
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2011-10-06
    • 2022-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多