473,661 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

session serialize format

Hi,
I am using a custom Session Handler.
session_set_sav e_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($_SESSI ON):

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

)
print_r($_SESSI ON); :

a:2:{s:4:"var1" ;s:5:"12345";s: 4:"var2";a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";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 4248
John Smith wrote:
My problem:
php don't uses the standard serialize function:
????? What? :)

(snip) print_r($_SESSI ON); :
I guess you mean
echo serialize($_SES SION);
a:2:{s:4:"var1" ;s:5:"12345";s: 4:"var2";a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";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($valu e);
$sql = "insert ... values('$serial ')";
// ...
?>
Why don't you simply do:
<?php
$sql = "insert ... values('" . mysql_escape_st ring(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:"var 1";s:5:"12345"; s:4:"var2";a:2: {s:6:"ebene1";s :2:"e1";s:6:"eb ene2";s:2:"e2"; }}

and the data in the database is:

var1|s:5:"123 45";var2|a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";s:2:"e2"; }:

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


If am using session_set_sav e_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_sav e_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:"1234 5";var2|a:2:{s: 6:"ebene1";s:2: "e1";s:6:"ebene 2";s:2:"e2";}:' ;
}
session_set_sav e_handler('OK', 'OK', 'read',
'OK', 'OK', 'OK');

/* *** */
session_start() ;
print_r($_SESSI ON);
?>

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_sav e_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($_SESSI ON):

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

)

print_r($_SESSI ON); :
a:2:{s:4:"var1" ;s:5:"12345";s: 4:"var2";a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";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='.$expir y.', value=\''.
$val.'\' WHERE sesskey=\''.$ke y.'\' AND expiry > '.time();
// if afected rows > 0
return strlen($val);
// else Session has expired
return FALSE;
// else
$query='INSERT INTO psa_sessions SET sesskey=\''.$ke y.
'\', expiry='.$expir y.', value=\''.$val. '\'';
return strlen($val);
}
session_set_sav e_handler('sess _open','sess_cl ose','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='.$expir y.', value=\''.
$val.'\' WHERE sesskey=\''.$ke y.'\' AND expiry > '.time();
// if afected rows > 0
return strlen($val);
// else Session has expired
return FALSE;
// else
$query='INSERT INTO psa_sessions SET sesskey=\''.$ke y.
'\', expiry='.$expir y.', value=\''.$val. '\'';
return strlen($val);
}
session_set_sav e_handler('sess _open','sess_cl ose','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.arco r-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_stri ng) {
if($c == '\\') {
.... etc etc...
}
}
}
}
Jul 17 '05 #7
John Smith <j.*****@gmx.de > wrote in message news:<40******* *************** *@newsread4.arc or-online.net>...
Hi,
I am using a custom Session Handler.
session_set_sav e_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($_SESSI ON):

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

)
print_r($_SESSI ON); :

a:2:{s:4:"var1" ;s:5:"12345";s: 4:"var2";a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";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.c om/groups?selm=not e-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($_SES SION); :

a:2:{s:4:"var 1";s:5:"12345"; s:4:"var2";a:2: {s:6:"ebene1";s :2:"e1";s:6:"eb ene2";s:2:"e2"; }}

and the data in the database is:

var1|s:5:"123 45";var2|a:2:{s :6:"ebene1";s:2 :"e1";s:6:"eben e2";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.c om/groups?selm=not e-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
2081
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 store, there is only one complicated object in this Session, but to get it on SessionStart to make a copy this is a problem. Maybe using Application or whatever? Or this data is divided and inaccessible anyway? Just D.
2
5033
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 storing the session in Sql Server. Please let me know if Iam doing anything wrong. Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when
5
5689
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 store the validated users DataRow into Session which is in SQLServer. I have listed the error below for you to
2
3939
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 connections in the finally block but I'm not sure what I should be doing. As far as the "Unable to serialize the session state" error, this app has been running for days and this is the first I've seen this one as well. Any thoughts would be...
4
5928
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 try to store more complex objects such as an ArrayList in session state I am not able to figure out exactly what I have to do to serialize and deserialize the object. There doesn't seem to be any code examples around when it comes to serializing...
2
2031
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 app individually, only one of the servers seems to return this error when I step through a sequence. I was told that I'm trying to store an object not marked as Serializable but I've tested this on a dev machine storing session variables without...
1
1468
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. Database.cs DatabaseFactory.cs DatabaseProviderFactory.cs DBCommandWrapper.cs
1
39693
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 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same
4
5693
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 problem I was facing is that sometimes I find the session and sometimes not. I beleive this is happenning because of multiple servers. Because session is created on a worker process on one server and the second time it must be hitting the other server...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.