Internet Explorer and Content-Type: text/plain

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Internet Explorer and Content-Type: text/plain

Post by McInfo »

Is there a way to force Internet Explorer (7) to display dynamically-generated pages that include a "Content-Type: text/plain" header the same way it displays static text files (files ending in ".txt")? In other words, force IE to display the content in the browser (the way Firefox does) instead of treating the content like a file.

text.php

Code: Select all

<?php
header('Content-Type: text/plain');
echo 'This should be displayed in the browser window, '
   . 'not opened by a separate application or saved as a file.';
?>
I tried adding a "Content-Disposition: inline" header to the script. That had no effect.

If it can be achieved by adding or changing headers, that would be acceptable, but I'm actually asking for a way to configure IE. I've looked through Internet Options without finding anything. I've also tried a few searches online without finding any exact matches to my question.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:47 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Internet Explorer and Content-Type: text/plain

Post by Eric! »

I think that IE like most MS products tries to help you too much. If it finds things that looks like HTML it renders them as HTML and ignores the content-type.

Do the pages you're generating have HTML in them? Or is your simple example without HTML also not working like you would expect?

FYI--I came across info about other people who had this problem some time ago when I was battling some other IE header problem...here's a link http://www.howtocreate.co.uk/wrongWithI ... xt%2Fplain

Also, I found some of the people at http://www.htmlforums.com/ to be pretty good with IE weirdness....so you could try them too.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Internet Explorer and Content-Type: text/plain

Post by McInfo »

Thanks for answering.

The files that exhibit this behavior are just like the example I posted. There is no HTML.

Just for thoroughness, I tried adding the "IsTextPlainHonored" key to the registry as is recommended to fix the text-as-HTML problem to see if that would fix my problem, but it had no effect.

One thing that does work (though it's not ideal) is changing the file extension from ".php" to ".txt" and telling Apache to send files with ".txt" extensions through the PHP parser.

.htaccess

Code: Select all

AddType application/x-httpd-php .txt
Like you say, this appears to be another case of Microsoft developers being too "helpful" but not being helpful enough to provide a way to be less "helpful".

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:48 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Internet Explorer and Content-Type: text/plain

Post by Eric! »

I just remembered something I worked on with mime-types and IE screwing it all up.

Try playing with options for these IE header types. Perhaps the noopen to open...?

Code: Select all

header("X-Download-Options: noopen"); // IE7 & IE8?
header("X-Content-Type-Options: nosniff"); // IE8 bonus?
When I looked into the sniffing thing again today I saw this article
https://connect.microsoft.com/IE/feedba ... kID=354921
Micro$oft wrote:At this time we do not plan on fixing this issue, part of the reasoning is that changing this behavior can impact existing applications. We appreciate the report, but unfortunately we are at a stage where need to choose what we work on to maximize the value for customers and web developers. For a workaround, please use 'X-Content-Type-Options: nosniff' header (http://blogs.msdn.com/ie/archive/2008/0 ... pdate.aspx)"
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Internet Explorer and Content-Type: text/plain

Post by McInfo »

Those headers looked promising, but no luck. I tried them individually and together with no change. IE still gives me a File Download dialog.

The X-Download-Options headers doesn't seem to apply to my problem.
AwghBlog wrote:The web server can set the response header “X-Download-Options” to the value “noopen”. This will tell Internet Explorer to only offer the option to save the file or cancel. It simply removes the “Open” option when this option is set.
Maybe I'll just go back to ignoring IE. I'm convinced that the only reason someone would choose to use IE on their personal computer is that they don't know that other browsers exist.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:49 pm, edited 1 time in total.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Internet Explorer and Content-Type: text/plain

Post by Eric! »

Bummer. Unfortunately there are a lot of people that don't know about other browsers. I've spent a lot of time trying to make things work in IE because of it. Sometimes I have to just go with the lowest common denominator and scale things back for IE.... :?

Do you have the same problem with IE8?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Internet Explorer and Content-Type: text/plain

Post by McInfo »

I haven't bothered to update IE for a while because I use it so infrequently. I just have version 7.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:49 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Internet Explorer and Content-Type: text/plain

Post by McInfo »

I have found a work-around of sorts. It's not exactly what I want, and not exactly a reliable fix for all users, but it fools IE into thinking it is requesting a text file so it displays the content as intended.

Just append a string like "/non-existent-text-file.txt" to the end of the URL. All that matters is that the string begins with "/" and ends with ".txt" so IE thinks it's a static text file. The string could even be just "/.txt". Apache will see that "real-file.php/non-existent-text-file.txt" does not match any file and will try to find "real-file.php". (I'm not sure if that's exactly how it works.) Because "real-file.php" exists, Apache serves it. IE doesn't know the difference.

Example:
http://localhost/book/page.php/.txt
One problem is that IE will cache the file. To prevent that, add a Cache-Control header.

Code: Select all

header('Cache-Control: no-cache');
Query strings will be forwarded to the PHP file.
http://localhost/book/page.php/.txt?id=123
Edit: Fixed spelling error

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:51 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Internet Explorer and Content-Type: text/plain

Post by califdon »

That's really bizarre. Glad you found a workaround, but I really hate using methods that "trick" the software into doing something else. Some day a programmer will do something that will kill it. I'm sure you feel the same way.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Internet Explorer and Content-Type: text/plain

Post by Eric! »

That's a good hack. :)
Post Reply