note 112973 added to function.session-start
| From: | ilnomedellaccount at gmail dot com | Date: | Tue, 13 Aug 2013 15:53:48 +0000 |
| Subject: | note 112973 added to function.session-start | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195957@lists.php.net to get a copy of this message | ||
A note about session_start(), custom handlers and database foreign key constraints, which I think
may be of some use...
We know that if we want our sessions into a database table (rather than the default storage), we can
refer to session_set_save_handler(...) to get them there. Note that session_set_save_handler must
(obviously) be called before session_start(), but let me get to the point...
Upon calling session_start() the "first time", when the session does not already exist,
php will spawn a new session but will not call the write handler until script execution finishes.
Thus, the session at this point exists in the server process memory, but won't be visible as a
row in the DB before the script ends.
This seems reasonable, because this avoids some unnecessary database access and resource usage
before we even populate our session with meaningfull and definitive data, but this also has
side-effects.
In my case, the script called session_start() to make sure a session was initiated, then used
session_id() to populate another table in the DB, which had foreign_key constraint to the
"sessions" table. This failed because no session was in the db at that point, yet!
I know I could simply force the creation of the row in the DB by manually calling the write handler
after session_start(), when necessary, but I am not sure if this is the best possible approach.
As soon as I find an "elegant" solution, or a completely different approach, I will post
some working sample code.
In the meanwhile... have fun!
----
Server IP: 69.147.83.199
Probable Submitter: 93.41.207.171
----
Manual Page -- http://php.net/manual/en/function.session-start.php
Edit -- https://master.php.net/note/edit/112973
Del: integrated -- https://master.php.net/note/delete/112973/integrated
Del: useless -- https://master.php.net/note/delete/112973/useless
Del: bad code -- https://master.php.net/note/delete/112973/bad+code
Del: spam -- https://master.php.net/note/delete/112973/spam
Del: non-english -- https://master.php.net/note/delete/112973/non-english
Del: in docs -- https://master.php.net/note/delete/112973/in+docs
Del: other reasons-- https://master.php.net/note/delete/112973
Reject -- https://master.php.net/note/reject/112973
Search -- https://master.php.net/manage/user-notes.php