Why is the Content Header 'application/javascript' causing a 500 Error? - php

I have a script that works fine on my test server (using IIS6). The script processes an ajax request and sends a response with the following line:
header( 'application/javascript' );
But on my live server, this line crashes the page and causes a 500 error.
Do I need to allow PHP to send different MIME types in IIS7? If so, how do I do this? I can't find any way on the interface.

The header is incorrect, try this instead:
header('Content-Type: application/javascript');

take a look at http://en.wikipedia.org/wiki/Mime_type
There it says you should use application/javascript instead of text/javascript.

Related

How to prevent Content-Type header from being added by web servers

I have a PHP script setting the Content-Type header to an empty value:
header('Content-Type: ');
This prevents nginx and Apache2 from setting the header, but the PHP development server sends the header even though it is empty.
I would like to know the best way to prevent this header from being added by web servers when the body of the response message is empty.
Thank you!
Maybe header_remove?
header_remove('Content-Type');

Error Serving WOFF File in PHP - file_get_contents versus readfile

I am trying to serve a WOFF font file in PHP.
First I set the Content-Type header:
header("Content-Type: application/font-woff");
Then I try the following:
This works fine (the file is properly served to the user, and the Content-Length header matches the file size):
readfile("path/to/font.woff");
But this doesn't (the script completes without error, but I receive an "octet stream" error in the browser, and the Content-Length header is 0):
echo file_get_contents("path/to/font.woff");
Why am I receiving an error using file_get_contents? I am using Windows 10, IIS Express 10, and PHP 7.2
You should check your server logs and your log_level. file_get_contents returns false if there was an error reading the file. I've tested it right now and echo(false) returns an empty string. In this case the response body would be empty (and the content length be 0).
The reason, that file_get_content is failing could either be an incorrect path or, that the file content cannot be interpreted as an string. File_get_contents tries to create a string out of the file. It could be, that this does not work with a woff file. But I'm not shure on this. You should check the logs (E_WARNING log level).

PHP MAMP and server conflicts

I have a page that calls a php script. On MAMP everything works fine but when I upload it to a server I get the following error:
Call Request failed! Status code: 4000
Reason - Caught an HttpRequestValidationException due to some bad characters in the request. Make sure your post request is encoded as xml, preferable as UTF-8 ('Content-Type: text/xml; charset=utf-8'). Exception: A potentially dangerous Request.Form value was detected from the client (<?xml version="..."utf-8"?> <uclassify xmlns="ht...").
Has anyone seen anything like that?
you can check it yourself here just place a word like php or ios
It looks like your server is validating based on the content-type header. It seems to want text/xml, whereas you are sending application/x-www-form-urlencoded (which is the default for $.ajax).
Try explicitly setting the content type to text/xml in your $.ajax call. (reference)
try changing charset=utf-8 to charset=UTF-8

With nginx/php-fpm, location header sometimes ignored by browser. Why?

I'm in the process of moving a working apache/mod-php website to nginx/php-fpm.
Under Apache/mod-php, I could use header("Location: $url"); to redirect the browser to a different page, such as after a login attempt. After switching to nginx/php-fpm, the browser would no longer follow that redirect on certain pages. I confirmed with Firebug and Httpfox that the header "Location: [url]" was actually being received in the response. This behavior also appears in Chrome (not tested in IE).
So I did a few experiments, read a few things about http, and made it work, but I'm not sure why it works (or why it didn't).
The solution I came up with was to send a "Status: 303" header before the "Location: [url]" header. This works in Chrome and Firefox, which both ignored the Location header when I sent "Status: 200" or omitted the Status header, but did the redirect when I changed it to "Status 303". It worked with Status 200 under Apache.
Is the Status header required to use the Location header? Or was Apache doing something else to make it work? I've not changed any of the php code involved other than the header("Status: 303"); line that made it work. There has to be something else at work here, but I have no clue what it could be.
Any ideas?
The Location header does not, by itself, trigger the browser to redirect. The redirect is actually triggered by an HTTP response code that is in the 3xx series. w3c has explanations for all http response codes.
Apache automatically sees the Location header in the response, and forces the response code to be 300-series if you haven't previously set a response code of your own. Nginx does not do this -- it expects you set the proper response code yourself.
You can force php to send a modified HTTP response code like this:
<?php
header("HTTP/1.0 301 Moved Permanently");
?>
...Then, of course, you'll need to be sure you still sent your Location header after sending the HTTP/1.0... line shown above.

Apache / PHP output caching

I'm working on a PHP script which generates large (multi-MB) output on the fly without knowing the length in advance. I am writing directly to php://output via fwrite() and have tried both standard output and using Transfer-Encoding: chunked (encoding the chunks as required) but no matter what I try the browser waits until all the data is written before displaying a download dialog. I have tried flush()ing too after the headers and after each chunk but this also makes no difference.
I'm guessing that Apache is caching the output as the browser would normally display after receiving a few kB from the server.
Does anyone have any ideas on how to stop this caching and flush the data to the browser as it is generated?
Thanks,
J
First of all, like BlaM mentioned in his comment, if in the PHP configuration OutputBuffering is enabled, it wont work, so it would be useful to know your phpinfo().
Next thing, try if it works with a big file that is stored on yor webserver, output it usinf readfile. And, together with this, check if you send the correct headers. Hints on how to readfile() and send the correct headers a provided here: StackOverflow: How to force a file download in PHP
And while you are at it, call ob_end_flush() or ob_end_clean() at the top of your script.

Categories