Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macmanmatty

macrumors regular
Original poster
Jul 10, 2005
237
0
(x,y)
I have recently installed php on my mac and Have few hopefully simple questions about making it work. When I embed php into an html file it only works if the extension is .php and not .html is their anyway to configure my web server to parse php in .html files?? or is this not reccomended to do? If ever my site should files containg php code be .php or .html??

Thanks

Macmanmatty
 
PHP File Extensions

PHP files can contain HTML, but HTML files cannot contain PHP. By that I mean a file containing PHP code must end in .PHP or another series of file extensions that are registered with APACHE (httpd.config)
You might be able to configure YOUR server to use .HTM/.HTML but I would advise against it, as nobody else will support that and you may wish to make your code portable
 
To be able to run php code in your browser you'll need a web server...like Apache. And you can use normal HTML in a .php file just as you would in .html, but because it is a .php file the web server will recognize that it has php in it and will be sure to run the php code appropriately. There is also .phtml I belive.
 
The main reason this is done is to keep people from knowing that a server is using PHP (or any other scripting language for that matter) by hiding the code in a seemingly static .html file.

To tell your webserver to parse .html files as PHP, find the relevant line in your Apache configuration file. For the default Apache installation on OSX, it will look something like the following:
Code:
<IfModule mod_php4.c>
    # If php is turned on, we repsect .php and .phps files.
    AddType application/x-httpd-php .php [B][COLOR="Red"].html[/COLOR][/B]
    AddType application/x-httpd-php-source .phps

    # Since most users will want index.php to work we
    # also automatically enable index.php
    <IfModule mod_dir.c>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>
Just add the .html extension (in bold, red above).

The default Apache configuration file is stored in /etc/httpd/httpd.conf. If you installed Apache yourself, the file names and locations might be different.

After you make the changes, just restart Apache and you will be in business.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.