0

I want to decode and use an base64 encoded array in php my code looks like this:

<?php
$hash = "YXJyYXkoInNhbXBsZV9pZCI9PiJObnJ5amZCV0p0STllalNRcnE2NHZIejFjc084RnVqUHRGRGk5WkdtM0Z3Vm9ieHprOSIsICJ4eF94eF94eHhfeHh4MSI9PiIwIiwgInNhbXBsZTIiID0+IjAiKQ==";
$hash_decoded = base64_decode($hash);
$all_infos = $hash_decoded;

$sample_id = $all_infos['sample_id'];
$xx_xx_xxx_xxx1 = $all_infos['xx_xx_xxx_xxx1'];
$sample2 = $all_infos['sample2'];
echo $sample_id;     ?>

and the decoded array is

array("sample_id"=>"NnryjfBWJtI9ejSQrq64vHz1csO8FujPtFDi9ZGm3FwVobxzk9", "xx_xx_xxx_xxx1"=>"0", "sample2" =>"0")

I can´t get the infos from the Array. The Console says

PHP Warning:  Illegal string offset 'sample_id' in [...] on line 6
PHP Warning:  Illegal string offset 'xx_xx_xxx_xxx1' in [...] on line 7
PHP Warning:  Illegal string offset 'sample2' in [...] on line 8
a

Where´s the problem? Thank´s for answers.

9
  • show all your code, not just what you believe is enough for us to guess what you did. Commented Feb 3, 2018 at 15:51
  • You're asking where's the problem.... I'm going to ask you what's the problem? You're pretty much saying that the base64 encoded data was decoded and you're even showing the result you got... and you're using it correctly. I don't know what's happening (or not happening) that makes you ask this question. Commented Feb 3, 2018 at 15:53
  • The way you have it right now, you need to $all_infos = eval('return '.$hash_decoded);, but this is a very bad idea. Instead, save your array using json_encode in the first place, instead of base64_encode, and save yourself a lot of trouble. Commented Feb 3, 2018 at 16:05
  • @Zeke Did you try running the code? Commented Feb 3, 2018 at 16:12
  • @AniketSahrawat the question did not have the errors when I commented on it. And no, I hadn’t, I just asked because nothing useful was there at the moment. Now there’s definitely more information. Commented Feb 3, 2018 at 16:14

3 Answers 3

1

The $all_infos variable is a string, since that is what you get back from base64_decode($hash). You cannot then expect it to become an array having properties like sample_id.

This particular string has a PHP expression encoded, but you'll need to interpret that string. One way is to use the infamous eval function. Take care to only use it when you trust the source of that string!

eval('$all_infos = ' . $hash_decoded . ";");
Sign up to request clarification or add additional context in comments.

3 Comments

You said i should only use it when i trust the source of the string but can i make the string safe when i use the stripslashes function?
You said i should only use this function when i trust the source of the string but can i make the string safe to use when i filter it with the stripslashes function?
No, that is no guarantee and might even make the evaluation fail. The source should provide that data as JSON, which is safe to decode.
1

I know it's an older question, but none of the answers seem to be correct and definitely the eval() is not recommended to be used.

As far as I see, the main issue is that you're directly encoding the array with base64_encode that convert it to a string without serializing in the first place the array (which will solve your issue); so, make sure you're using PHP's function before:

 serialize ( mixed $value ) : string

Below is a fully working example for your case:

// Step 1: Correctly format the original array with serialize to not lose their type and structure
$originalArray = ["sample_id"=>"NnryjfBWJtI9ejSQrq64vHz1csO8FujPtFDi9ZGm3FwVobxzk9", "xx_xx_xxx_xxx1"=>"0", "sample2" =>"0"];
// first serialize the array and then base64_encode it
$hashedArray =  base64_encode(serialize($originalArray));
// output : YTozOntzOjk6InNhbXBsZV9pZCI7czo1MDoiTm5yeWpmQldKdEk5ZWpTUXJxNjR2SHoxY3NPOEZ1alB0RkRpOVpHbTNGd1ZvYnh6azkiO3M6MTQ6Inh4X3h4X3h4eF94eHgxIjtzOjE6IjAiO3M6Nzoic2FtcGxlMiI7czoxOiIwIjt9
print_r($hashedArray);


// Step 2: Re-decoding the hash to use the array
$hash = "YTozOntzOjk6InNhbXBsZV9pZCI7czo1MDoiTm5yeWpmQldKdEk5ZWpTUXJxNjR2SHoxY3NPOEZ1alB0RkRpOVpHbTNGd1ZvYnh6azkiO3M6MTQ6Inh4X3h4X3h4eF94eHgxIjtzOjE6IjAiO3M6Nzoic2FtcGxlMiI7czoxOiIwIjt9";
$hashDecoded = unserialize(base64_decode($hash));
//output : Array ( [sample_id] => NnryjfBWJtI9ejSQrq64vHz1csO8FujPtFDi9ZGm3FwVobxzk9 [xx_xx_xxx_xxx1] => 0 [sample2] => 0 ) 
print_r($hashDecoded);
// Getting the information you want:
$sample_id = $hashDecoded['sample_id'];
$xx_xx_xxx_xxx1 = $hashDecoded['xx_xx_xxx_xxx1'];
$sample2 = $hashDecoded['sample2'];

Comments

0

To decode base64 encoded elements of array use this PHP code:

$array = ["sample_id"=>"NnryjfBWJtI9ejSQrq64vHz1csO8FujPtFDi9ZGm3FwVobxzk9"];
array_walk($array, 'array_decode');
function array_decode(&$item) {
     $item = base64_decode($item);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.