Search code examples
phpjsonbase64mcrypt

json_decode($string, true) returns a string of 'Array' instead of the Array itself


I believe the problem is that I am base64_encoding the json string and when I base64_decode the string and run json_decode($string, true) for some reason it just returns the string of 'Array'.

Here is where I pass the json to be encoded:

$data = '[{"id":"1","name":"Dave","email":"dave@test.com","password":"1610838743cc90e3e4fdda748282d9b8","isAdmin":"true","timeStamp":"2012-09-18 20:37:38"}]';

Session::set('user', $data, true);

Here is my base64 encode/decode code:

public static function set($key, $value, $encrypt = false) {
    if ($encrypt == false) {
        $_SESSION[$key] = $value;
    } else {
        $_SESSION[$key] = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5(SALT1), $value, MCRYPT_MODE_CBC, md5(md5(SALT2))));
    }
}

public static function get($key, $decrypt = false) {
    if (isset($_SESSION[$key])) {
        if ($decrypt == false) {
            return $_SESSION[$key];
        } else {
            return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5(SALT1), base64_decode($_SESSION[$key]), MCRYPT_MODE_CBC, md5(md5(SALT2))), "\0");
        }
    }
}

Then here is where I make the call to decode the value:

$user = Session::get('user', true);

echo json_decode($user, true);

And my result is:

Array

I have looked into the different mcrypt modes, but was unsuccessful in finding a solution. Any help would be greatly appreciated. Thanks.


Solution

  • The echo just outputs an string. you will have to use print_r() or var_dump() to view the array.

    print_r(json_decode($user, true));
    

    or use

    var_dump(json_decode($user, true));