PHP script not working in HTML file

可紊 提交于 2019-11-27 01:38:40

XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.

I assume you are trying to use php inside .html file? Try adding .htaccess file or changing apache config with the following line:

AddHandler application/x-httpd-php .html
user2301506

Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...

AddType application/x-httpd-php .html .htm  

Restart apache!

This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.

You should add mime type at http conf for instance in apache at httpd.conf entry

<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig "conf/mime.types"
   .......
    AddType application/x-httpd-php .html .htm
   AddType text/html .shtml

    AddOutputFilter INCLUDES .shtml
</IfModule>

The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>

which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandler is fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess file to change the configuration for that directory (and its subdirectories).

Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.

The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.

Here is a simple example of proof:

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Blank Web Page</title>

    <link rel="stylesheet" type="text/css" href="css/css.css">

</head>

<body>

    <?php

    $son = 5;
    $nos =10;

    echo $son + $nos;

    ?>

    <h4>test to see if this html element can be output too!</h4>

    <script type="text/javascript" src="js/js.js"></script>

</body>

Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.

I forgot to add that this works for Xampp too.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!