Saturday, May 29, 2010

Custom Session handler with DB issue

Alright, I've racked my brain on this far too long now and want to see
if I can get another set of eyes on my code to see if I'm missing
something.

I need a custom handler for my sessions for a couple reasons:

1) My app is going between different domains, not subdomains, and
requires the referrer check to be set differently.

2) The high security setting in Cake has always given me issues with
session regeneration, so I need to set it to medium, but I want to
keep the cookie lifetime setting of 0 so cookies die after closing the
browser. Medium doesn't allow for this.

3) I need database sessions so that when my app does go back and forth
between domains, session data can be read from the DB and copied from
one session to the other on each of the domains.

Having said that, this is what I have so far (I put it in the bin to
make it easier to read):

http://bin.cakephp.org/view/263734852

I copied what I thought was everything from the cake_session.php file
in regards to database session handling to be implemented in my own
handler. Unfortunately, everytime the session starts, meaning on every
page, I'm seeing the following debug messages:

Warning (2): mysqli_real_escape_string() [function.mysqli-real-escape-
string]: Couldn't fetch mysqli [CORE/cake/libs/model/datasources/dbo/
dbo_mysqli.php, line 201]
Code | Context

$data = "4uenk81stnmcmuec2kk3v7lmm3"
$column = "string"
$safe = false
$parent = null

mysqli_real_escape_string - [internal], line ??
DboMysqli::value() - CORE/cake/libs/model/datasources/dbo/
dbo_mysqli.php, line 201
DboSource::__parseKey() - CORE/cake/libs/model/datasources/
dbo_source.php, line 2265
DboSource::conditionKeysToString() - CORE/cake/libs/model/datasources/
dbo_source.php, line 2207
DboSource::conditions() - CORE/cake/libs/model/datasources/
dbo_source.php, line 2087
DboSource::buildStatement() - CORE/cake/libs/model/datasources/
dbo_source.php, line 1447
DboSource::generateAssociationQuery() - CORE/cake/libs/model/
datasources/dbo_source.php, line 1240
DboSource::read() - CORE/cake/libs/model/datasources/dbo_source.php,
line 810
Model::find() - CORE/cake/libs/model/model.php, line 2087
Model::exists() - CORE/cake/libs/model/model.php, line 1979
Model::save() - CORE/cake/libs/model/model.php, line 1242
CakeSession::__write() - CORE/cake/libs/cake_session.php, line 756
[main] - [internal], line ??

Warning (2): mysqli_query() [function.mysqli-query]: Couldn't fetch
mysqli [CORE/cake/libs/model/datasources/dbo/dbo_mysqli.php, line 115]
Code | Context

$sql = "SELECT COUNT(*) AS `count` FROM `cake_sessions` AS `Session`
WHERE `Session`.`id` = '' "

mysqli_query - [internal], line ??
DboMysqli::_execute() - CORE/cake/libs/model/datasources/dbo/
dbo_mysqli.php, line 115
DboSource::execute() - CORE/cake/libs/model/datasources/
dbo_source.php, line 250
DboSource::fetchAll() - CORE/cake/libs/model/datasources/
dbo_source.php, line 407
DboSource::read() - CORE/cake/libs/model/datasources/dbo_source.php,
line 812
Model::find() - CORE/cake/libs/model/model.php, line 2087
Model::exists() - CORE/cake/libs/model/model.php, line 1979
Model::save() - CORE/cake/libs/model/model.php, line 1242
CakeSession::__write() - CORE/cake/libs/cake_session.php, line 756
[main] - [internal], line ??

Warning (2): mysqli_errno() [function.mysqli-errno]: Couldn't fetch
mysqli [CORE/cake/libs/model/datasources/dbo/dbo_mysqli.php, line 214]
Code

mysqli_errno - [internal], line ??
DboMysqli::lastError() - CORE/cake/libs/model/datasources/dbo/
dbo_mysqli.php, line 214
DboSource::execute() - CORE/cake/libs/model/datasources/
dbo_source.php, line 254
DboSource::fetchAll() - CORE/cake/libs/model/datasources/
dbo_source.php, line 407
DboSource::read() - CORE/cake/libs/model/datasources/dbo_source.php,
line 812
Model::find() - CORE/cake/libs/model/model.php, line 2087
Model::exists() - CORE/cake/libs/model/model.php, line 1979
Model::save() - CORE/cake/libs/model/model.php, line 1242
CakeSession::__write() - CORE/cake/libs/cake_session.php, line 756
[main] - [internal], line ??

I can see that the session cookie is started properly in the browser,
but when I check the database, the session hasn't been started.
Obviously, this is in debug mode and I've used bake to build out the
db schema for cake_sessions, so that should all be setup properly.
There's got to be some other reason in my code that's causing this
breakdown. This is all using version 1.3.1. There was a custom session
handling bug in 1.3.0 that was causing my code to give a PHP fatal
error. 1.3.1 fixed that, but now I'm getting the issues outlined above
instead.

Any help would be greatly appreciated.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: