Koozali.org: home of the SME Server

Obsolete Releases => SME Server 7.x => Topic started by: girkers on October 31, 2006, 04:00:28 AM

Title: PHP not excuting inside HTML page
Post by: girkers on October 31, 2006, 04:00:28 AM
When I run this page: phptest.php
Code: [Select]
<?php

   $value 
$_SERVER['REMOTE_ADDR'&#93;;
   
print key&#40;$_SERVER&#41; . "&#58; $value";

?>


I get the desired result, but when I put that script inside of a HTML file phptest.html it appear not to be parsed through the PHP processor.
Code: [Select]
<html>
    <head>
        <title>Example</title>
    </head>
    <body>

    <?php

   
$value $_SERVER['REMOTE_ADDR'&#93;;
   
print key&#40;$_SERVER&#41; . "&#58; $value";

?>


    </body>
</html>


I have even take a simple example straight from the PHP manual and this is not parsed. On both occassions I simply get a blank page with the title of the page.

I have checked both the messages and httpd.error_log logs and there is nothing in them. Any clues?
Title: PHP not excuting inside HTML page
Post by: owen on October 31, 2006, 07:22:20 AM
I'm just taking a stab in the dark here, but IIRC Apache looks at the file extension to know what to do with it.

So if you have a file index.html, it will simply serve it to the clients browser without putting it through mod_php to interpret the code, because it thinks its just static html. Whereas if it is a .php file then it will be parsed before before being served.

So either you should use the extension of .php if you want it to be parsed as php, or you need to change the setting in httpd.conf (keeping in mind the whole template thing) to tell Apache to parse .html files as php too.

Of course I could be totally wrong, in which case I'm sure someone will correct me.
 8)
Title: PHP not excuting inside HTML page
Post by: cactus on October 31, 2006, 10:48:19 PM
Quote from: "owen"
I'm just taking a stab in the dark here, but IIRC Apache looks at the file extension to know what to do with it.

So if you have a file index.html, it will simply serve it to the clients browser without putting it through mod_php to interpret the code, because it thinks its just static html. Whereas if it is a .php file then it will be parsed before before being served.

So either you should use the extension of .php if you want it to be parsed as php, or you need to change the setting in httpd.conf (keeping in mind the whole template thing) to tell Apache to parse .html files as php too.

Of course I could be totally wrong, in which case I'm sure someone will correct me.
 8)
You are completely right. HTML files are not run throught the parser.. PHP files are, even if there is plain HTML code in there. I suggest that you simply rename HTML files with PHP code in it to have the xetension of PHP to get them interpretated by the server.
Title: PHP not excuting inside HTML page
Post by: girkers on October 31, 2006, 11:38:01 PM
You are great men!  :D You were right as simple as that, cheers
Title: PHP not excuting inside HTML page
Post by: piran on November 01, 2006, 12:50:08 AM
Alternatively you could put this clause into .htaccess.
Code: [Select]

<Files phptest.html>
ForceType application/x-httpd-php
</Files>
Title: PHP not excuting inside HTML page
Post by: CharlieBrady on November 01, 2006, 12:56:18 AM
Quote from: "piran"
Alternatively you could put this clause into .htaccess.
Code: [Select]

<Files phptest.html>
ForceType application/x-httpd-php
</Files>


No, that will not do anything. Apache in SME is configured to ignore .htaccess files (as a security precaution).

You can make such changes if required in httpd.conf template fragments - but surely using .php file extensions is easiest.
Title: PHP not excuting inside HTML page
Post by: piran on November 01, 2006, 01:01:45 AM
<...but surely using .php file extensions is easiest.>
Agreed. I mentioned it only as an alternative as I don't know the original
poster's full predicament. I currently use the technique to present dynamic
robots.txt files ie on a selective basis depending on various conditions.
Title: PHP not excuting inside HTML page
Post by: girkers on November 09, 2006, 04:33:14 AM
Renaming the file was my solution in the end and it works a treat, all the CSS and HTML is left and the PHP is executed perfectly.

Thanks again.