![](/img/trans.png)
[英]PHP, which is the better way between storing data into session and database?
[英]Storing session data better bytea or text type column in database
我會將會話數據存儲在數據庫中,但是我不確定使用哪種類型的列更好(我將存儲序列化的對象和數組)。 我認為更好的方法是使用BYTEA原因,當我幾天前嘗試使用命名空間存儲對象時,php的pdo失去了頭(從命名空間斜杠)。 為了解決問題,它在BYTEA列中存儲了對象,並在綁定值時告訴php使用類型PDO :: PARAM_LOB。
$this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB);
但是此參數僅適用於BYTEA列(Postgresql)。 因此,最好選擇BYTEA列類型而不是TEXT ??? 有興趣知道何時使用此列類型嗎?
我將不勝感激。
更新:
我將使用session_set_save_handler函數和SessionHandlerInterface實現我的會話處理程序系統。 它看起來像:
class SessionHandler{
public function open($save_path, $session_name) {// some code}
public function close() {//some code}
public function read($id) {// some code}
public function write($id, $data) {
// Here i would implement mechanism to store object into database using php pdo.
// Here variable $data is already serialized by php session mechanism and ready to put into database.
// But i have unpleasantly experience storing serialized object
// using pdo client with namespaces thought bindParam function.
// This experience is:
// The serialized object (string) with namespace was cut at half,
// when i tried used PDO::PARAM_STR as argument in $sth->bindValue() (TEXT column i database).
// When to the same operation i used PDO::PARAM_LOB (BYTEA column in database) it was stored all fine.
}
public function destroy($id) {//some code}
public function gc($maxlifetime) {// some code}
}
$hnadler = new SessionHandler()
session_set_save_handler($handler, true);
而在經歷之后,我不確定使用哪個庫侖。
更新:
感謝cleaver的回答克雷格·林格(Craig Ringer):在您發表文章后,我決定去檢查一下php手冊中有關對象序列化的確切內容:
http://www.php.net/manual/zh/function.serialize.php
序列化功能
Returns a string containing a byte-stream representation of value
that can be stored anywhere.
Note that this is a binary string which may include null bytes,
and needs to be stored and handled as such. For example, serialize()
output should generally be stored in a BLOB field in a database,
rather than a CHAR or TEXT field.
因此,您建議將對象存儲在BYTEA類型列而不是TEXT中是好的。
如果要存儲序列化的應用程序數據,那么bytea
是合適的選擇。 它只是字節,對於提供它的應用程序來說,除了它不是文本 ,從某種意義上說,它不是惡意字符,對任何東西都沒有意義。
因此,出於數據建模的原因,應使用bytea
。
另外,如果數據可能包含空字節,則必須使用bytea
,因為text
不能包含空字節,並且將數據編碼為(例如)base64以解決該問題是效率低下且令人恐懼的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.