solinent Posted November 14, 2006 Share Posted November 14, 2006 From my main site the follow snippet of code to get the users currently online and sort (sessions are stored in db properly).$db is just a wrapper so that I can switch dbs and make things easier for myself. $db->recordSet[''] will contain the data within the current record (it works, I've used it in other places).This is the code.[code]<?php$i=0;while ($db->sql_array()) { $sessionDat = unserialize($db->recordSet['Data']); if (is_array($sessionDat)) { print_r($sessionDat); $user[$i] = $sessionDat['username']; echo "{".$user[$i]."}"; } else { print_r(unserialize($db->recordSet['Data'])); die("not array".$db->recordSet['SessionID']."{".$db->recordSet['Data']."}"); } $i++;}sort($user);?>[/code]It produces this output (die() is called).[code]not array43b3119f405da246159cbf23ac494a1b{test|s:4:"test";username|s:8:"solinent";}[/code]The Data field is returning a weird value (it is used in conjunction with sesssion_set_save_handler). When I run the following code:[code]<pre><?php// Serialize an array$serialized_data = serialize (array ('test' => 'uno', 'dos', 'tres'));// Show what the serialized data looks likeprint $serialized_data . "\n\n";// Unserialize the data$var = unserialize ($serialized_data);// Show what the unserialized data looks like.var_dump ($var);?></pre>[/code]It gives me this output:[code]a:3:{s:4:"test";s:3:"uno";i:0;s:3:"dos";i:1;s:4:"tres";}array(3) { ["test"]=> string(3) "uno" [0]=> string(3) "dos" [1]=> string(4) "tres"}[/code]What's wrong? Quote Link to comment Share on other sites More sharing options...
printf Posted November 14, 2006 Share Posted November 14, 2006 Check the database and see if that row field Data is messed up, if it is then it your save session handler! Quote Link to comment Share on other sites More sharing options...
solinent Posted November 14, 2006 Author Share Posted November 14, 2006 Data is stored by setting $_SESSIONHere's my sess.php: (I only use $db in one place because I thought it was a problem -- I was debugging. It does work, and I can show you my simple $db class (minus connect).[code]<?php if (!defined('def_sess')) { define('def_sess', true); require_once("db.class.php"); $db = new db; $db->connect(); function sess_open($sess_path, $sess_name) { return true; } function sess_close() { return true; } function sess_read($sess_id) { global $db; $db->query("SELECT Data FROM sessions WHERE SessionID = '$sess_id';"); if (!$db->count_rows()) { $CurrentTime = time(); $db->execute("INSERT INTO sessions (SessionID, DateTouched) VALUES ('$sess_id', $CurrentTime);"); return ''; } else { extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess'); $db->execute("UPDATE sessions SET DateTouched = $CurrentTime WHERE SessionID = '$sess_id';"); return $sess_Data; } } function sess_write($sess_id, $data) { $CurrentTime = time(); mysql_query("UPDATE sessions SET Data = '$data', DateTouched = $CurrentTime WHERE SessionID = '$sess_id';"); return true; } function sess_destroy($sess_id) { mysql_query("DELETE FROM sessions WHERE SessionID = '$sess_id';"); $GLOBALS['destroied'] = true; return true; } function sess_gc($sess_maxlifetime) { $CurrentTime = time(); mysql_query("DELETE FROM sessions WHERE DateTouched + $sess_maxlifetime < $CurrentTime;"); return true; } session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); session_start(); }?>[/code]EDIT: SOLVED! I looked through PHP.net comments and found this nifty function! Weird :S[code]function unserialize_session_data( $serialized_string ) { $variables = array( ); $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); for( $i = 0; $i < count( $a ); $i = $i+2 ) { $variables[$a[$i]] = unserialize( $a[$i+1] ); } return( $variables );}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.