0

I am writing the following html code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

<script type="text/javascript">
    function detectBrowser(){
        if(navigator.appName === "Microsoft Internet Explorer"){
            window.open("redirect.html", "_parent");
        }
    }
</script>
</head>

<body onload="detectBrowser()">
<div class="mainBody">  
<?php
    echo "test";
?>
</div>
</body>
</html>

But the php block doesn't display test for me. It looks like it is not parsed and I can see the php code in the webpage's source. Any one can tell the problem?
Thank you

12
  • 7
    Is the file saved with extension .php? Is your server running PHP? Commented Jan 31, 2013 at 21:19
  • 3
    Your web server is not configured correctly. Commented Jan 31, 2013 at 21:19
  • 3
    Doesn't matter, change the extension to .php. It will still be HTML! But you need to be on a web server that is configured with PHP. Commented Jan 31, 2013 at 21:20
  • 1
    you need to save it as a .php in order for the php to show Commented Jan 31, 2013 at 21:20
  • 1
    You should check so you are using a .php extension or else the webserver don't know that the file should be parsed as php. If you would like to use another extension like .html this can be done by using a AddType directive in a .htaccess file. Make sure you also show the extension of all files if you are using windows. The file should also be run through a installed server, do you have one? Commented Jan 31, 2013 at 21:22

2 Answers 2

3

You can enable parsing of PHP in files with .html extensions in your httpd.conf file.

Look for a line like this and make sure .html is an option.

 AddType application/x-httpd-php .php .php3 .phtml .html 

You could probably add it to an .htaccess file in the docroot of you site as well, but I've not tried this personally.

Sign up to request clarification or add additional context in comments.

1 Comment

While this works, it's probably a bad idea (the server probably serves other pure-html files which should not go through the PHP parser). Changing the extension of the file would be simpler and safer. If you really want .html in your url, you can fake it with a RewriteRule. Using the "phtml" extension might also be an acceptable compromise.
1

You could change this in your .htaccess file. (if it doesn't exist just create the file (text only) in the same folder as your file)

AddType application/x-httpd-php .html

However this would make all static files also go through the PHP parser which is unnecessary.

I would suggest the best solution is to rename the file instead with extension .php. Then in your .htaccess file you could rewrite just this address, then you will be able to access it with the .html extension anyway.

RewriteEngine on
RewriteRule ^myfile.html$ myfile.php 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.