Symfony2.3 PDOHandler在请求中创建了太多会话

时间:2014-11-10 13:30:28

标签: php symfony session pdo

我正在使用Symfony2.3及其内置的PDOSessionHandler工具来存储数据库中的会话。出于某种原因,似乎在每个请求上都有多个会话被写入数据库,因此,我无法登录。这是我的PDO会话配置:

parameters:
    pdo.db_options:
        db_table:    Session
        db_id_col:   id
        db_data_col: session_value
        db_time_col: session_time    
services:
    pdo:
        class: PDO
        arguments:
            dsn:      "mysql:host=%database_host%;port=%database_port%;dbname=%database_name%"
            user:     %database_user%
            password: %database_password%
        calls:
            - [ setAttribute, [3, 2] ] # \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION

    session.handler.pdo:
        class:    Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
        arguments:
            - "@pdo"
            - "%pdo.db_options%"

假设我作为匿名用户向https://acme.mybundle.com/app_dev.php发出一个GET请求。然后,这是我将在Session表中看到的输出示例:

+----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+
| id | session_value                                                                                                                                                                                                                                                                    | session_time |
+----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+
|  1 | X3NmMl9hdHRyaWJ1dGVzfGE6MTp7czozNDoiX3NlY3VyaXR5LnNlY3VyZWRfYXJlYS50YXJnZXRfcGF0aCI7czozODoiaHR0cHM6Ly9hbGxlbmNvLmRldi5hdmF0YXJuZXd5b3JrLmNvbS8iO31fc2YyX2ZsYXNoZXN8YTowOnt9X3NmMl9tZXRhfGE6Mzp7czoxOiJ1IjtpOjE0MTU2MjU4ODQ7czoxOiJjIjtpOjE0MTU2MjU4ODQ7czoxOiJsIjtzOjE6IjAiO30= |   1415625884 |
|  2 | X3NmMl9hdHRyaWJ1dGVzfGE6MDp7fV9zZjJfZmxhc2hlc3xhOjA6e31fc2YyX21ldGF8YTozOntzOjE6InUiO2k6MTQxNTYyNTg4NDtzOjE6ImMiO2k6MTQxNTYyNTg4NDtzOjE6ImwiO3M6MToiMCI7fQ==                                                                                                                     |   1415625884 |
+----+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+

如果您需要,请输入我的security.yml文件:

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Acme\MyBundle\Entity\User: sha512
    providers:
        main:
            id: acme.user.provider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login_firewall:
            pattern: ^/login$
            security: false
        secured_area:
            pattern: ^/
            form_login: ~
            logout:
                path:   /logout
                target: /
    access_control:
        - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
        - { path: ^/, roles: ROLE_USER, requires_channel: https }

经过1-5次尝试后,我通常可以登录,不再为以后的任何请求创建会话。最后,如果相关,我们的服务器在代理服务器后面。有谁知道为什么会在每个请求上写入多个会话?谢谢!

1 个答案:

答案 0 :(得分:0)

原来问题出在我的ORM Session实体上。我有$id的以下条目:

<?php

namespace Acme\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Session
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Session
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

//...
}

会话的$id不应该是一个整数 - 它应该是一个字符串(显然它也不应该是自动递增的。发现这个bug后我有很多文件)。将我的课程的$id条目重写为:

/**
 * @var string
 *
 * @ORM\Column(type="string", length=255)
 * @ORM\Id
 */
private $id;

并且正在运行php app/console doctrine:schema:update --force,一切似乎都正常运行。