Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 431
Abs Avatar asked Dec 11 '25 04:12

Abs


1 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.

like image 113
Joe Mills Avatar answered Dec 12 '25 18:12

Joe Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!