By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,492 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,492 IT Pros & Developers. It's quick & easy.

session serialize format

P: n/a
Hi,
I am using a custom Session Handler.
session_set_save_handler is working well.

But i want to read the data direct from the database.
My problem:
php don't uses the standard serialize function:

For example:

print_r($_SESSION):

Array
(
[var1] => 12345
[var2] => Array
(
[ebene1] => e1
[ebene2] => e2
)

)
print_r($_SESSION); :

a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6:"e bene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:

var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1";s :6:"ebene2";s:2:"e2";}:

Can anybody help me?
Anybody has an idea how to make an array from the database data?

Thanx John
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
John Smith wrote:
My problem:
php don't uses the standard serialize function:
????? What? :)

(snip) print_r($_SESSION); :
I guess you mean
echo serialize($_SESSION);
a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6:"e bene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:

var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1";s :6:"ebene2";s:2:"e2";}:
This appears to have been put into the database with
<?php
$serial = '';
foreach ($_SESSION as $name=>$value)
$serial .= $name . '|' . serialize($value);
$sql = "insert ... values('$serial')";
// ...
?>
Why don't you simply do:
<?php
$sql = "insert ... values('" . mysql_escape_string(serialize($_SESSION)) . "')";
// ...
?>
Can anybody help me?
Anybody has an idea how to make an array from the database data?


Either serialize (and unserialize) the whole $_SESSION, or you're going
to have a hard time to parse the data in the database to repopulate the
$_SESSION array.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
John Smith wrote:
a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6: "ebene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:

var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1" ;s:6:"ebene2";s:2:"e2";}:

This appears to have been put into the database with
<?php
$serial = '';
foreach ($_SESSION as $name=>$value)
$serial .= $name . '|' . serialize($value);
$sql = "insert ... values('$serial')";
// ...
?>
Why don't you simply do:
<?php
$sql = "insert ... values('" . mysql_escape_string(serialize($_SESSION)) . "')";
// ...
?>


If am using session_set_save_handler.
http://www.php.net/session_set_save_handler

Php serializes the data, not my script.

The script only writes and reads the data form/to the database.

Ciao John
Jul 17 '05 #3

P: n/a
John Smith wrote:
If am using session_set_save_handler.

Php serializes the data, not my script.

The script only writes and reads the data form/to the database.


I had never looked at how the string is formatted by PHP before it
reaches the write function. My PHP formats it the same as yours.

I did a small script just to verify that that is right.

<?php
/* handler functions */
function OK($p1='', $p2='') { return true; }
function read($dummy) {
return 'var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1"; s:6:"ebene2";s:2:"e2";}:';
}
session_set_save_handler('OK', 'OK', 'read',
'OK', 'OK', 'OK');

/* *** */
session_start();
print_r($_SESSION);
?>

Works as expected with PHP 4.3.3.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

P: n/a
John Smith wrote:
I am using a custom Session Handler.
session_set_save_handler is working well.

But i want to read the data direct from the database.

My problem:
php don't uses the standard serialize function:

For example:

print_r($_SESSION):

Array
(
[var1] => 12345
[var2] => Array
(
[ebene1] => e1
[ebene2] => e2
)

)

print_r($_SESSION); :
a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6:"e bene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:
var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1";s :6:"ebene2";s:2:"e2";}:

Can anybody help me?
Anybody has an idea how to make an array from the database data?


Here are the basics of the functions that I use:

function sess_read($key){
$query='SELECT value FROM psa_sessions WHERE sesskey=\''.
$key.'\' AND expiry > '.time();
}

function sess_write($key,$val){
global $SESS_LIFE;
// Calculate the session end time
$expiry=time()+$SESS_LIFE;
$query='SELECT sesskey FROM psa_sessions WHERE sesskey=\''.
$key.'\'';

// if exists
$query='UPDATE psa_sessions SET expiry='.$expiry.', value=\''.
$val.'\' WHERE sesskey=\''.$key.'\' AND expiry > '.time();
// if afected rows > 0
return strlen($val);
// else Session has expired
return FALSE;
// else
$query='INSERT INTO psa_sessions SET sesskey=\''.$key.
'\', expiry='.$expiry.', value=\''.$val.'\'';
return strlen($val);
}
session_set_save_handler('sess_open','sess_close', 'sess_read','sess_write','sess_destroy','sess_gc') ;

My handlers are all abstracted for Metabase and specific to a package I
wrote (with a lot of file-logging lines in it), so I think giving you
the entire session handler include file would be more confusing that
helpful.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
SEO Competition League: http://seo.koivi.com/
Jul 17 '05 #5

P: n/a
Justin Koivisto wrote:
Here are the basics of the functions that I use:

function sess_read($key){
$query='SELECT value FROM psa_sessions WHERE sesskey=\''.
$key.'\' AND expiry > '.time();
}

function sess_write($key,$val){
global $SESS_LIFE;
// Calculate the session end time
$expiry=time()+$SESS_LIFE;
$query='SELECT sesskey FROM psa_sessions WHERE sesskey=\''.
$key.'\'';

// if exists
$query='UPDATE psa_sessions SET expiry='.$expiry.', value=\''.
$val.'\' WHERE sesskey=\''.$key.'\' AND expiry > '.time();
// if afected rows > 0
return strlen($val);
// else Session has expired
return FALSE;
// else
$query='INSERT INTO psa_sessions SET sesskey=\''.$key.
'\', expiry='.$expiry.', value=\''.$val.'\'';
return strlen($val);
}
session_set_save_handler('sess_open','sess_close', 'sess_read','sess_write','sess_destroy','sess_gc') ;


Thanx for your answer,
but that's not the problem.

I am seraching for a solution how to
turn the session string
var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1";s :6:"ebene2";s:2:"e2";}:
to an array.
Standard serialize doesn't work.
Ciao John
Jul 17 '05 #6

P: n/a

Uzytkownik "John Smith" <j.*****@gmx.de> napisal w wiadomosci
news:40***********************@newsread4.arcor-online.net...
Can anybody help me?
Anybody has an idea how to make an array from the database data?


I tried something like that once without success. The main problem is that
PHP doesn't escape the ';' in the serialization string, so you can't just do
an explode(';', ... ) to separate the string into name|serialized-string
pairs. Maybe we can first escape the semi-colons with slashes, then do a
preg_split with a lookbehind check...or write a char-by-char parser a la C.

for($i = 0, $l = strlen($s); $i < $l; $++) {
$c = $s{$i};
if(!$state) {
if($c == '|') {
$state = 1;
}
else {
$var_name .= $c;
}
}
else {
if($inside_string) {
if($c == '\\') {
.... etc etc...
}
}
}
}
Jul 17 '05 #7

P: n/a
John Smith <j.*****@gmx.de> wrote in message news:<40***********************@newsread4.arcor-online.net>...
Hi,
I am using a custom Session Handler.
session_set_save_handler is working well.

But i want to read the data direct from the database.
My problem:
php don't uses the standard serialize function:

For example:

print_r($_SESSION):

Array
(
[var1] => 12345
[var2] => Array
(
[ebene1] => e1
[ebene2] => e2
)

)
print_r($_SESSION); :

a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6:"e bene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:

var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1";s :6:"ebene2";s:2:"e2";}:

Can anybody help me?
Anybody has an idea how to make an array from the database data?


Unfortunately, nowadays vandals badly deletes some useful usernotes
from php.net <http://groups.google.com/groups?selm=note-23908%40php.net>
Fortunately, Google has archived this page. It is worth to note that
Google totally stopped archiving php.* groups.

--
"Success is not what you achieve, but it is what you die for"
If you live in USA, please support John Edwards.
Email: rrjanbiah-at-Y!com
Jul 17 '05 #8

P: n/a
R. Rajesh Jeba Anbiah wrote:


print_r($_SESSION); :

a:2:{s:4:"var1";s:5:"12345";s:4:"var2";a:2:{s:6: "ebene1";s:2:"e1";s:6:"ebene2";s:2:"e2";}}

and the data in the database is:

var1|s:5:"12345";var2|a:2:{s:6:"ebene1";s:2:"e1" ;s:6:"ebene2";s:2:"e2";}:

Can anybody help me?
Anybody has an idea how to make an array from the database data?

Unfortunately, nowadays vandals badly deletes some useful usernotes
from php.net <http://groups.google.com/groups?selm=note-23908%40php.net>
Fortunately, Google has archived this page. It is worth to note that
Google totally stopped archiving php.* groups.


Thanx, it's working.
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.