0

I need an array in multiple pages, so I tried to make a php file where the array is printed. I used:

file_put_contents('file.php',
    '<?php $order = ' . var_export($order1, true) . '; ?>');

I was hoping that if I included this file in the files where I need the array, I could use it as $order, but I get an error instead. I checked file.php and everything looks in order. Does anyone knows what I am doing wrong?

Functino
  • 1,939
  • 17
  • 25
BenBen
  • 15
  • 2

1 Answers1

1

You should not create .php files by your backend. It is not secure. I recommend you to use JSON for that:

      file_put_contents('file.json',json_encode($order1));
      $order = json_decode(file_get_content($url), true);

And you should hide this files from web in your .htaccess or by another available method for secure purpose.

  • array `$order1` is a multidimensional array containing objects, is it possible that the objects become arrays after the decode? Because I can't recall `$idA = $order1[$Q][0]->id;` anymore. – BenBen Sep 13 '15 at 10:27
  • `$order = json_decode(file_get_content($url), false);` but everything will be an object (last parametr in json_decode goes to false) – Alexander Shcheglakov Sep 13 '15 at 10:28
  • Also you can use "serialize" not the `json_decode`: `file_put_contents('file.json', serialize($order1)); $order = unserialize(file_get_content($url));` – Alexander Shcheglakov Sep 13 '15 at 10:32