-2

I am taking a Web Design and Management class and the professor basically wants us to upload a website into the university's student server. I have done websites before, but he wants us to incorporate PHP in it, for which I have virtually no experience. I wrote a page, index.html:

<html>
    <head>
        <title>Welcome</title>
    </head>

    <?php include('../includes/header.php') ?>
    <?php include('../includes/menu.php') ?>
    <?php include('../includes/lsidebar.php') ?>
    <?php include('../includes/rsidebar.php') ?>
    <?php include('../includes/home.php') ?>

</html>

Once I uploaded this file, along with all the pertaining ones (header.php, menu.php, lsidebar.php, rsidebar.php, and home.php all with some html content), and change proper file permissions to 755, I opened the space reserved for me in the server to find the index page was blank!

I e-mailed the professor about this, and he said I should place this line as my first line in the file:

#!/usr/local/bin/php

I ran the site again, and the page only displays the above line. How can I enable PHP commands to be recognized inside an HTML file?

Thank you very much for any assistance in advance!

gfcf14
  • 316
  • 4
  • 30

1 Answers1

2

First of all, as @FoX commented before, you have to add the .php extension to your files in order to run PHP code on the server side. If you add .html extension, PHP code is not capable to run on the server side.

EDIT: Unless you configurate your server to do it in that way, as @Fred-ii- suggested in his comment. In that case, you will have to edit the .htaccess file (Apache) or the web.config file in (Windows IIS server) to treat .html files as .php.

He also suggested another option, that one was based on header modification through httpd.conf file in Apache. I attach the links he provided me:

https://encodable.com/parse_html_files_as_php/

Parse HTML as PHP

In addition, your code will debug Fatals errors due the fact that each line must end with a ;

<?php include('../includes/header.php'); ?>
<?php include('../includes/menu.php'); ?>
<?php include('../includes/lsidebar.php'); ?>
<?php include('../includes/rsidebar.php'); ?>
<?php include('../includes/home.php'); ?>

I would suggest you to take a look at the basics concepts of the PHP at the W3Schools website:

http://www.w3schools.com/php/

Community
  • 1
  • 1
Llogari Casas
  • 942
  • 1
  • 13
  • 35
  • 1
    *"If you add .html extension, PHP code is not capable to run on the server side (Obviously)."* - Untrue. Read [my second comment](http://stackoverflow.com/questions/30305705/cannot-load-php-inside-html-webpage#comment48706780_30305705) to the OP. – Funk Forty Niner May 18 '15 at 14:30
  • 1
    That's fair, I forgot to add, "_unless you use `.htaccess` or `web.config` on your server in order to treat `.html` files as `.php`_" :) @Fred-ii- – Llogari Casas May 18 '15 at 14:48
  • 1
    It should be added to your answer. Plus, here's a Q&A http://stackoverflow.com/q/7181853/ that shows a few other options. Another page https://encodable.com/parse_html_files_as_php/ – Funk Forty Niner May 18 '15 at 14:54
  • 1
    Thanks for the links! I usually use `mod_rewrite` to deal with that kind of situations. It's good to know another option. As you suggested, I edited the answer @Fred-ii- – Llogari Casas May 18 '15 at 15:05