PHP script not working in HTML file

后端 未结 6 1983
北海茫月
北海茫月 2020-11-27 06:22

I\'m new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP\'s htdocs and got PHP to display in my browser. My question is, why does my PHP

相关标签:
6条回答
  • 2020-11-27 06:47

    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.

    0 讨论(0)
  • 2020-11-27 06:50

    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).

    0 讨论(0)
  • 2020-11-27 06:52

    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>
    
    0 讨论(0)
  • 2020-11-27 06:53

    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.

    0 讨论(0)
  • 2020-11-27 06:57

    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
    
    0 讨论(0)
  • 2020-11-27 07:07

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

    0 讨论(0)
提交回复
热议问题