Comments (5)
It certainly sounds like the db is silently truncating the value. What column type? Size? Which database? Accessed over the network or via socket (if the former and MySQL, check max_allowed_packet). Anything unusual about the property names in your entity that the db encoding isn't going to handle?
Originally posted by @kynx at zendframework/zend-session#64 (comment)
from laminas-session.
Database is PostgreSQL 9.4. Session data column: session_data text NOT NULL
.
I dumped serialized $userEntity:
\Zend\Debug\Debug::dump(serialize($userEntity));
and got this:
:6:{s:36:"\000User\Entity\UserEntity\000userId";N
What are those zeros? According to google it's NULL char but where they come from? Most probably they are a problem here.
<?php
namespace User\Entity;
class UserEntity
{
/**
* @var int
*/
private $userId;
php -v
PHP 7.0.12 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.12, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans
Edit: I did some test and use json_encode/decode in Zend\Session\SaveHandler\DbTableGateway
. Now session data is correct and I receive full entity. But how can I fix it natively?
Originally posted by @xorock at zendframework/zend-session#64 (comment)
from laminas-session.
Ok. After some more reading everything is clear now.
http://php.net/manual/en/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.
I did some test on pure PDO object with PDO::PARAM_LOB and bytea field, serializing/unserializing my entity and full object was stored and returned.
My problem was due to incorrect behaviour of Zend\Session\SaveHandler\DbTableGateway which always converts data to string and it is not possible to store it as PARAM_LOB. Also, I don't know how to pass PARAM_LOB to Zend\Db\TableGateway.
Possible solution: convert data to json (many db engines have appropriate fields) or use bin2hex/pack and store as BLOB. It can not be just serialize/unserialize because it will throw PDOException on bytea field and on text field, it will cut string on first null byte.
Originally posted by @xorock at zendframework/zend-session#64 (comment)
from laminas-session.
I'd never noticed the NULLs around private property names either. But then I tend to avoid serialising objects in session. Personally I would just save the userId rather than the whole object. It'll be easy enough to fetch the entity back from the DB.
Originally posted by @kynx at zendframework/zend-session#64 (comment)
from laminas-session.
Of course there are many workarounds. For now, I have just extended DbTableGateway to work on JSON data. I had some bad experiences with objects and ZF1 Zend_Session, but because I'm rewriting my entire app I wanted to do it right. Fetching back user entity with every request doesn't make sense. More natural would be writing data to the array and hydrate when needed. Still, it's a bug in Db save handler but also in the documentation that suggests the use of a text field.
Originally posted by @xorock at zendframework/zend-session#64 (comment)
from laminas-session.
Related Issues (20)
- SessionManager doesn't call rememberMe() automatically HOT 3
- Remove default session manager as it introduces hidden dependencies HOT 4
- Session gc_maxlifetime and cookie_lifetime with php-fpm pool config
- Implement count() from ArrayObject in Container HOT 1
- Tests for SessionManager preserveStorage flag
- [Feature] Allow Session validators to be created using the service manager
- Add ValidatorServiceInterface for session validators
- Lazy session start HOT 3
- Support PHP7's read_and_close HOT 14
- Use PHP ext mongodb instead of mongo for v3 HOT 10
- Create containers from request objects rather than from superglobals HOT 9
- Introduce session.cookie_samesite HOT 1
- Remove exception thrown when session validation fails HOT 3
- Psalm integration HOT 1
- PHP 8.0 support
- Possible unescaped special chars in session name and bug after PHP patch #79699 HOT 2
- Missing dependency on laminas-http HOT 1
- clear() does not clear metadata for provided key
- Switch from Travis-Ci to GitHub actions for continuous integration workflow
- Dependency Dashboard
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laminas-session.