1

I am trying to make a page that returns a file if the page is being requested by a user, and returns a api response if requested by code. It works fine but google chrome (and ie) render the user page as if it was:

<head></head>
<body><title>BLAH</title>
<h1>Bloo bloo blah</h1></body>

Even though view source returns:

<head><title>BLAH</title></head>
<body><h1>Bloo bloo blah</h1></body>

but when I go to the page that is being included, it displays improperly.

PHP code:

<?php
if ($_GET['mode']) {
    echo 'server response';
}
else {
    include_once('main.php');
}
?>

main.php code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>BLAH</title></head>
<body><h1>Bloo bloo blah</h1></body> 
</html>

Any help would be appreciated. I am using IIS 6.1 with PHP 5.3.5 running on a Windows 7 computer.

comp500
  • 354
  • 4
  • 14

1 Answers1

1

Please try the following:

Instead of

if ($_GET['mode']) {

check if a value for the key mode is set before using it:

if (isset($_GET['mode']) && $_GET['mode']) {

That's just a precaution in case PHP generates output because of a warning or notice (but must not), which can influence the output then.

Next to that you should actually check what the server is returning, not what view source in the browser gives you. Install the cUrl commandline tool (Download) and then run in a shell:

curl http://myurl

It will output the HTML as your server returns it. This information is actually needed before your problem can be further analyzed.

Edit: The cause of your problem is that main.php is saved with a BOM. Remove the BOM from the file and you're fine.

Related:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836