Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > I hate PHP sessions... why are they so finiky

I hate PHP sessions... why are they so finiky
Thread Tools
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Feb 22, 2005, 03:23 AM
 
I'm in the process of taking all of my loose functions just lying around in my site and putting them in utility classes. This includes session stuff...

It all was working fine, but my database session handlers were not working... ok so I turned off the db handling and writing to a session file worked fine. Then I got the db handlers fixed and writing the session to the db was working fine...

Sessions were working fine... well I come back to work on the code some more, and to my surprise, all of the sudden sessions are only writing part of the data... to either the db or session file. And they are updating because when I use the database, the expiration time changes, but only part of the session data is there...

What really irks me, is that it was working perfectly fine not a few hours ago when I stopped to take a break, and come back and sessions are suddenly only partially working.

Let me be a little more clear. The only session data that is remember and saved, is session data set in the 1st file included. For example, this is basically my /index.php page:

[php]<?php
require('includes/application_top.php');

require(DIR_WS_PAGES . 'main.php');

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>[/php]

Only session data set in application_top.php is being saved when the session closes... I don't get it... none of the other session info set in the main script is being saved now... but it was just not that long ago... but now it isn't...

If it helps any, here is my session utility class:

[php]<?php
require_once(dirname(__FILE__) . '/utilities.php');

// Session Handler
if (STORE_SESSIONS == 'database') {
if (!$SESS_LIFE = get_cfg_var('session.gc_maxlifetime')) {
$SESS_LIFE = 1440;
}

if (!session_set_save_handler(
array('Session', 'dbOpen'),
array('Session', 'dbClose'),
array('Session', 'dbRead'),
array('Session', 'dbWrite'),
array('Session', 'dbDestroy'),
array('Session', 'dbGC')
)) {
trigger_error('session_set_save_handler() failed ', E_USER_ERROR);
}
}

class Session extends UtilitiesClass {

function start() {
return session_start();
}

function ID($sessid = '') {
if (!empty($sessid)) {
return session_id($sessid);
} else {
return session_id();
}
}

function name($name = '') {
if (!empty($name)) {
return session_name($name);
} else {
return session_name();
}
}

function close() {
return session_write_close();
}

function closeExit() {
Session::close();

exit();
}

function destroy() {
return session_destroy();
}

function savePath($path = '') {
if (!empty($path)) {
return session_save_path($path);
} else {
return session_save_path();
}
}

function dbOpen($save_path, $session_name) {
return true;
}

function dbClose() {
return true;
}

function dbRead($sesskey) {
global $db;

$db->query(QueryHelper::select('value', TABLE_SESSIONS, "sesskey = '" . $sesskey . "' and expiry > '" . time() . "'"));

$read_value = $db->getNext();

if (String::notNull($read_value['value'])) {
return $read_value['value'];
}

return false;
}

function dbWrite($sesskey, $value) {
global $SESS_LIFE, $db;

$expiry = time() + $SESS_LIFE;

$db->query(QueryHelper::select('count(*) as total', TABLE_SESSIONS, "sesskey = '" . $sesskey . "'"));

$count = $db->getNext();

if ($count['total'] > 0) {
$update_sql_array = array(
'expiry' => $expiry,
'value' => $value,
);

return $db->query(QueryHelper::update(TABLE_SESSIONS, $update_sql_array, "sesskey = '" . $sesskey . "'"));
} else {
$insert_sql_array = array(
'sesskey' => $sesskey,
'expiry' => $expiry,
'value' => $value,
);

return $db->query(QueryHelper::insert(TABLE_SESSIONS, $insert_sql_array));
}
}

function dbDestroy($sesskey) {
global $db;

return $db->query(QueryHelper::delete(TABLE_SESSIONS, "sesskey = '" . $sesskey . "'"));
}

function dbGC($maxlifetime) {
global $db;

return $db->query(QueryHelper::delete(TABLE_SESSIONS, "expiry < '" . time() . "'"));
}

}

?>[/php]

If anyone has any ideas I sure would appreciate it.
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
     
madmacgames  (op)
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Feb 22, 2005, 03:44 AM
 
Ah nevermind, I am the idiot... I have been staring at the screen too long. Sessions are working fine.
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Feb 23, 2005, 12:51 PM
 
If had a penny for everytime I've done that; spent hours and hours on a problem, going over and over it, searching and searching the internet, and then posting what seems to be a well complicated question on a message board such as this.

Not long after a lightbulb appears above one's head and one's back on track.

It almost seems like the answer can't be found without posting about it first, sort of like how you'll never find that lost dogbrush without first buying a replacement.

There must be something to be said for sitting down and writing what the problem entails that helps the thought process along.

Or someit like that.
     
Love Calm Quiet
Mac Elite
Join Date: Mar 2001
Location: CO
Status: Offline
Reply With Quote
Feb 24, 2005, 09:39 AM
 
I'll second Skalie.

That being said...

Sessions *do* seem to be such a complex hassle to set up (not to mention debug).

Ah, for a nifty way to automate (at least simplify) *that* process!

It would sure open up PHP to those of with out the gift of programming.
TOMBSTONE: "He's trashed his last preferences"
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 12:33 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,