PHP Storing Sessions: Can't Seem to Serialize Session Variables

I am making use of seralize and unseralize to set and get session variables from my Database.

A user is in a session and every time they click save. I do this:

$array = serialize($_SESSION);
//and save to DB field

When a user loads a session, I load the variables too to continue that session like so:

//get row from DB
$_SESSION = unserialize($row['session_variables']);
  1. This doesn't work for me. It firstly doesn't unseralize as it returns something like this when I print_r($_SESSION):

    Array (
        [user_id] => test2
        [date_created] =>
        [date_updated] =>
        [session_variables] => a:9:{s:7:"user_id";s:5:"test2";s:12:"date_created";N;s:12:"date_updated";N;s:17:"session_variables";s:149:"a:6:{s:7:"user_id";s:5:"test2";s:4:"here";s:2:"12";s:5:"here2";s:6:"112432";s:5:"here3";s:6:"132432";s:5:"here4";s:4:"1qw2";s:5:"here5";s:5:"1wqe2";}";s:4:"here";s:2:"12";s:5:"here2";s:6:"112432";s:5:"here3";s:6:"132432";s:5:"here4";s:4:"1qw2";s:5:"here5";s:5:"1wqe2";}
        [here] => 12
        [here2] => 112432
        [here3] => 132432
        [here4] => 1qw2
        [here5] => 1wqe2
    )
    
  2. Where is the session_id for these variables to be used across different pages? Have I over written them?

Thanks all for any help

EDIT

Is the session_id kept in the global $_SESSION? I am guessing no. If I unset $_SESSION, it means the session will not be gone just the variables, correct? Anyone verify please?

Answers


Try this.

$array = base64_encode(serialize($_SESSION)); // going to the database
$_SESSION = unserialize(base64_decode($row['session_vars'])); // coming from the database

Often times MySQL will not play nice with serialized data unless you base64_encode it. See if that helps.


Need Your Help

Android Client - Java Server: how many clients can a server support at the same time?

java android mysql jdbc client-server

I did not find any adequate answers to my question on Internet. So, I am asking here.

Splits in hadoop with variable-length/non-delimited binary file

hadoop gis openstreetmap hadoop-partitioning

I've just started working on a hadoop based ingester for open street map data. There are a few formats - but I've been targeting a protocolbuffer based format (note - it's not pure pb).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.