air
air

Reputation: 6264

Can we include php file in html file?

How can we include a php file into a html file?

I know we can do this with:

  <AddType application/x-httpd-php .html>

but I don't have access to the server. Any other solution which I can include in my website folder or file?

Upvotes: 0

Views: 4848

Answers (4)

Julius F
Julius F

Reputation: 3444

The correct solution is not renaming it to .php. It is renaming to .phtml ;)

.phtml is the official file type for crossover files containing PHP and HTML code.

Upvotes: 0

Pekka
Pekka

Reputation: 449385

Depending on what the code looks like that you want to include, and if you really have no other choice like the ones presented by Druid, you can use one of the following less-than-perfect embedding solutions:

  • An IFRAME with frameborder="0" pointing to the PHP script (Downside: dynamic resizing according to output size is possible only with JavaScript

  • An AJAX Snippet that loads the PHP file onLoad and injects the output into a DIV of your choice

  • A tag pointing to the PHP file. The PHP file must then issue JavaScript commands like "document.write()".

The IFRAME is the most fail-safe in the event the client has no JavaScript. The AJAX Snippet is the most elegant solution.

Upvotes: 1

Druid
Druid

Reputation: 6453

If you have access to your website folder, you could create a .htaccess file that includes:

<IfModule mod_mime.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html
</IfModule>

You could alternatively try:

<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch> 

Upvotes: 3

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

Why not just rename the file to end in PHP? That would be the simplest way to do this.

Upvotes: 4

Related Questions