将用户会话数据存储到自定义数据库中

时间:2021-11-10 16:58:46

It's possible to store user session in database by changing the settings in factories.yml:

通过更改factories.yml中的设置,可以将用户会话存储在数据库中:

all:
  storage:
    class: sfMySQLSessionStorage
    param:
      db_table:    session              # Name of the table storing the sessions
      database:    propel               # Name of the database connection to use
      # Optional parameters
      db_id_col:   sess_id              # Name of the column storing the session id
      db_data_col: sess_data            # Name of the column storing the session data
      db_time_col: sess_time            # Name of the column storing the session timestamp

From the example given, it seems that the columns in the table are limited to a few predefined ones. Which means that it's not possible to store other information that Symfony doesn't anticipated, such as ip address.

从给出的示例中,似乎表中的列仅限于几个预定义的列。这意味着无法存储Symfony未预料到的其他信息,例如ip地址。

Is it possible to extend the database schema to include more custom columns?

是否可以扩展数据库架构以包含更多自定义列?

2 个解决方案

#1


You could create your own session class like this

您可以像这样创建自己的会话类

class myPDOSessionStorage extends sfPDOSessionStorage {
    /**
    * Extending session write function
    *
    * @param string $id Session identifier  
    * @param mixed $data Session's data
    */
    public function sessionWrite($id, $data){
        /*
          Handle your specific data, e.g. :
        */
        $mySession = new MySession();
        $mySession->setIp($_SERVER['REMOTE_IP']);
        // connect with org. session
        $mySession->setSessionId($id);
        // Save extended session - you for sure need to add logic in finding existing one's
        $mySession->save();

        // IMPORTANT Call Parent function to update original session data
        parent::sessionWrite($id, $data);
    }
}

In factories.yml :

在factories.yml:

all:
  storage:
    class: myPDOSessionStorage
    db_table:    my_session # Name of the table storing the sessions
    database:    my_session # Database connection to use

This was a basic example, so you'll find further informations here :

这是一个基本示例,因此您可以在此处找到更多信息:

#2


Not directly; the structure of a session is very limited, though you can store quite a bit of data within the session. Symfony just stores the session data as a blob in the db. You could try modifying the sfMySQLSessionStorage class to include the $_SERVER['REMOTE_IP'] in the session table if you really need it.

不直接;虽然您可以在会话中存储相当多的数据,但会话的结构非常有限。 Symfony只将会话数据存储为db中的blob。如果确实需要,可以尝试修改sfMySQLSessionStorage类以在会话表中包含$ _SERVER ['REMOTE_IP']。

#1


You could create your own session class like this

您可以像这样创建自己的会话类

class myPDOSessionStorage extends sfPDOSessionStorage {
    /**
    * Extending session write function
    *
    * @param string $id Session identifier  
    * @param mixed $data Session's data
    */
    public function sessionWrite($id, $data){
        /*
          Handle your specific data, e.g. :
        */
        $mySession = new MySession();
        $mySession->setIp($_SERVER['REMOTE_IP']);
        // connect with org. session
        $mySession->setSessionId($id);
        // Save extended session - you for sure need to add logic in finding existing one's
        $mySession->save();

        // IMPORTANT Call Parent function to update original session data
        parent::sessionWrite($id, $data);
    }
}

In factories.yml :

在factories.yml:

all:
  storage:
    class: myPDOSessionStorage
    db_table:    my_session # Name of the table storing the sessions
    database:    my_session # Database connection to use

This was a basic example, so you'll find further informations here :

这是一个基本示例,因此您可以在此处找到更多信息:

#2


Not directly; the structure of a session is very limited, though you can store quite a bit of data within the session. Symfony just stores the session data as a blob in the db. You could try modifying the sfMySQLSessionStorage class to include the $_SERVER['REMOTE_IP'] in the session table if you really need it.

不直接;虽然您可以在会话中存储相当多的数据,但会话的结构非常有限。 Symfony只将会话数据存储为db中的blob。如果确实需要,可以尝试修改sfMySQLSessionStorage类以在会话表中包含$ _SERVER ['REMOTE_IP']。