Saturday, March 5, 2011

How to execute PHP code in html page

When we access a webpage the server decides how to pass the page information to the client. If the page is an html page (.html) the server sends the data direct to the client. But if the page is a server script page ( eg : .php ) the server process the page and send the output to the user.

If we have an html page and we would like to embed some PHP code in that page, usually its not possible by changing the file extension. But using .htaccess file we can make it possible without changing the file extension. Create a .htaccess file and add the following code.
AddType application/x-httpd-php .html
Upload this file into the root folder.

Now the server processes the .html page as .php file.

Example html code

Page name: sample.html
<html>
<head><title>Run PHP from html page</title></head>
<body>
<?php echo "this is from index.html"; ?>
</body>
</html>

No comments:

Post a Comment