Jump to content

Weird SERIALIZE and UNSERIALIZE problem!


solinent

Recommended Posts

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 like
print $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?
Link to comment
Share on other sites

Data is stored by setting $_SESSION

Here'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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.