homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Can I run PHP inside a .html file?

 6:45 pm on May 26, 2004 (gmt 0)

I know that a PHP file needs a .PHP extension, but my web logs tell me that customers are landing on an HTML file and I would love to access my MySQL database from there?

Can this be done?



 6:48 pm on May 26, 2004 (gmt 0)

You can parse your html pages as PHP. Use .htaccess to do it.


 6:51 pm on May 26, 2004 (gmt 0)

Yes, there are literally hundreds of threads on this topic - you can use the site search to find them.

Look for "php addtype".


 9:28 pm on May 26, 2004 (gmt 0)

if you have server side includes turned on, use an include call to a php file:

<!--#include virtual="myfile.php" -->

You can include any scripting processing in the php file. The server completes any server side scripting (database calls etc) in the php file before it is called into your .html file.


 11:56 am on May 27, 2004 (gmt 0)

include virtual won't work if the page has an extension of " .html"
(at least on my servers)
using include virtual calls for an extension of:


 2:34 pm on May 27, 2004 (gmt 0)

I thought SHTML files work for CGI. I will try the include method. I use tons of includes in my PHPs, but never in my HTMl. I will have to change that habit!

Thanks everybody.

If you have anymore suggestions, please don't hesitate to throw them in here.


 6:40 am on May 30, 2004 (gmt 0)

There are a few different ways to include php into an HTML document.

1. If you have available to you cpanel or the windows equivalent then you can simply turn on SSI, add .htm and .html to your list and begin to include SSI's within your html pages.
<!--#include virtual="myfile.php" -->

2. You can reference a small javascript
<script language="javaScript" src="myfile.php"></script>
Do note that the "type" has been left out on purpose.
Using the method you will also have to modify your echo statments as well. Sometimes they have a funny effect if you don't break them up. If you don't have any problems then don't fix it... if you start to have problems then look at your echo statements

3. This is just the opposite....
If you want to send information to a php file such as the title of the webpage
You can utilize the following method

<script language="JavaScript">
var image = "../pixel.gif";
var title = escape(document.title);
var link = "<img src='../myphpfile.php?title=" + title + "&img=" + image + "' align='middle' style='display:none' alt='&copy;0'>";
document.write(link); //This will write out to the page

Now on your PHP page you include the following:
$title = $_GET['title']

That will transfer the information from JavaScripting via the URL to the PHP page and now you can do what you want with it via PHP.

Hope it helps


[edited by: jatar_k at 3:48 pm (utc) on May 30, 2004]
[edit reason] no sigs thanks [/edit]


 7:18 am on May 30, 2004 (gmt 0)

I want to use a normal include statement but I want to use .html files. How can I change my .htaccess file to do this?


 9:30 am on May 30, 2004 (gmt 0)

see [webmasterworld.com ]


[edited by: corz at 9:32 am (utc) on May 30, 2004]


 9:31 am on May 30, 2004 (gmt 0)

henry0 / ogletree

Your include statement is only working on shtml files because your .htaccess file (if you are allowed to use one) needs to be altered. Here is a sample .htaccess file command to allow includes for .htm .html and .shtml

AddType text/x-server-parsed-html .shtml .html .htm

Other unrelated uses for this .htaccess file may be as follows:

ErrorDocument 404 [mydomain.co.uk...]

this gives a smooth 404 redirect, also if your home page does not happen to be a default one, use this:

DirectoryIndex myfile.php

There are other commands you can add to the .htaccess file including modrewrite statements. Below is a compilation of the ones above which can be pasted into a text file, amended, and saved as .htaccess and tried:

AddType text/x-server-parsed-html .shtml .html .htm
ErrorDocument 404 [mydomain.co.uk...]
DirectoryIndex myfile.php


 9:36 am on May 30, 2004 (gmt 0)

That is just too cool thank you.

[edited by: ogletree at 9:41 am (utc) on May 30, 2004]


 9:38 am on May 30, 2004 (gmt 0)

Add this to the .htaccess file in the directory that you want your html files to act like php files:

AddType application/x-httpd-php .html



Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved