JosephLenton / PHP-Error

Better error reporting for PHP, and prettier too!
1.35k stars 151 forks source link

Need ability to turn off "magic JS" prepend from within application #22

Closed christianmagill closed 11 years ago

christianmagill commented 12 years ago

In sending generated csv files to the user to save, I set headers and use readfile. I am ending up with the this "magic JS" prepended to my files. I have tired executing the code through withoutErrors with the same results.

JosephLenton commented 12 years ago

What headers are you setting when you offer a CSV file for download? If you set the 'content-type' in the header to 'text/csv', then this should solve the problem. i.e.

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo "record1,record2,record3\n";

A second solution is to turn this behaviour off, by setting 'catch_ajax_errors' to false. This is what outputs the JS code.

The reason the JS code is still added when you use 'withoutErrors', is because PHP Error is turned off, you are setting the error, and then it gets turned back on when 'withoutErrors' ends. At that point, the magic JS is then allowed again, and so still gets outputted.

christianmagill commented 12 years ago

Thanks for the reply... I have been using

header('Content-type: application/csv');

Christian Magill

On Sat, Sep 8, 2012 at 11:08 PM, JosephLenton notifications@github.comwrote:

What headers are you setting when you offer a CSV file for download? If you set the 'content-type' in the header to 'text/csv', then this should solve the problem. i.e.

header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=file.csv"); header("Pragma: no-cache"); header("Expires: 0");

echo "record1,record2,record3\n";

A second solution is to turn this behaviour off, by setting 'catch_ajax_errors' to false. This is what outputs the JS code.

The reason the JS code is still added when you use 'withoutErrors', is because PHP Error is turned off, you are setting the error, and then it gets turned back on when 'withoutErrors' ends. At that point, the magic JS is then allowed again, and so still gets outputted.

— Reply to this email directly or view it on GitHubhttps://github.com/JosephLenton/PHP-Error/issues/22#issuecomment-8400244.

JosephLenton commented 12 years ago

That should do it too, so there might be a bug in here somewhere.

JosephLenton commented 11 years ago

Just a guess, but there was an error with Wordpress a few days ago where the content-type turned out to be 'text/html; utf-8' instead of just 'text/html'.

It could be that the encoding type was being appended onto your header type.

That bug has been fixed, so it might have fixed your original issue too.