Koozali.org: home of the SME Server

PHP not excuting inside HTML page

Offline girkers

  • *
  • 296
  • +0/-0
    • gk computer services
PHP not excuting inside HTML page
« 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?

owen

PHP not excuting inside HTML page
« Reply #1 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)

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
PHP not excuting inside HTML page
« Reply #2 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.
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline girkers

  • *
  • 296
  • +0/-0
    • gk computer services
PHP not excuting inside HTML page
« Reply #3 on: October 31, 2006, 11:38:01 PM »
You are great men!  :D You were right as simple as that, cheers

Offline piran

  • *****
  • 502
  • +0/-0
PHP not excuting inside HTML page
« Reply #4 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>

Offline CharlieBrady

  • *
  • 6,918
  • +3/-0
PHP not excuting inside HTML page
« Reply #5 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.

Offline piran

  • *****
  • 502
  • +0/-0
PHP not excuting inside HTML page
« Reply #6 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.

Offline girkers

  • *
  • 296
  • +0/-0
    • gk computer services
PHP not excuting inside HTML page
« Reply #7 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.