Skip to content Skip to sidebar Skip to footer

Improve Page Performance, Save PHP Array On Server?

is it possible to store a PHP-array to my server, right now it always gets created when someone reloads the page from a CSV file but that is unnecessary since the file only chances

Solution 1:

After you parse your CSV, do this:

$file = fopen('/tmp/output.js', 'w');
fwrite($file, '<script type="text/javascript">');
fwrite($file, 'var array =');
fwrite($file, json_encode( $array ));
fwrite($file, ';');
fwrite($file, '</script>');
fclose($file);

copy('/path/to/script.js', '/path/to/script.js.bak');
move('/tmp/output.js', '/path/to/script.js');

Then, later on when you are outputting the HTML, you just need to stick in a:

<script type="text/javascript" src="/scripts/script.js">

in the header. People's browsers should cache it properly too. Note the copy and move -- you don't strictly need to make a backup copy, but you MUST use a move() to replace the 'live' script -- move() is atomic, more or less, and won't result in anyone getting a half-file.

Also, note that you'll need write permissions to where the script is -- there are ways to keep this pretty secure (not letting your PHP script write all over the hard drive), but that's out of scope here.


Solution 2:

Since you mention getting the data on an hourly basis I suggest the following:

  1. grab the CSV file with cron and store the data in a database on an hourly basis
  2. configure your data tables component to use server side data

This way you won't force every user to download the entire array at once on every first page load. The server side script only fetches the number of records that need to be displayed on that particular page in the table.


Post a Comment for "Improve Page Performance, Save PHP Array On Server?"