473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

session serialize format

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
8 4237
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
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Just D. | last post by:
Do we have any access to the Session object from a different Session? The idea is to save Session of a current user and then if he logs in again then return the Session back. It's not a problem to...
2
by: jakk | last post by:
Below is the exception that Iam getting. It says that the DataView that Iam storing in the session is not Serializable. BUt works fine if I store in the inproc session and fails if I switch to...
5
by: sdettmers | last post by:
Hi, Database: SQL Server Session: SQL Server Language: C# Application: ASP.Net I have created a login page which attempts to retrieve the users record from the database and I...
2
by: Dave | last post by:
We just started getting this error message in our application today (stack trace below). From the OutOfMemoryException, I'm guessing it could be a memory leak. I'm making sure I'm closing all my...
4
by: Gary | last post by:
Hello, I have implemented SQL Server session state in my application however I am having some problems. Simple objects stored in Session state are fine and are handled by .NET, however when I...
2
by: Dave | last post by:
I have an application running on a 3 server webfarm running Windows 2003 with SQLServer Session state. After running for several hours, I started getting the following error. When I access each...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
1
by: Tim | last post by:
Could anyone tell me what this means and how do I correct it. Any suggestions? Thanks! Tim Richardson IT Developer and Consultant www.paladin3d.com Unable to serialize the session state. In...
4
by: NAT | last post by:
I was using session mode as "InProc"(entered in web.config). I have deployed my ASP.NET appln. on a server which uses Load Balancer. i.e I have two servers. I am using session across pages.The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.