-1

I realize this question is similar to this one but there didn't seem to be a definitive answer there. Currently I'm using <?php include "banner.html"; ?> but in order for this to work I need to change the ext of ALL pages to .php instead of .html.

I don't think this is really best practice. I'd prefer to keep my basic webpages as html files and reserve php files for server-side logic - isn't that the way it's supposed to be??

The banner should be displayed at the top of each page by default as soon as the page loads, so from what I've read jquery.load() is not appropriate as that should typically be triggered by user input. This is not an ASP.NET project so master pages is not an option. Also heard about HTML templating but after googling it I walked away more confused than ever.

Community
  • 1
  • 1
user1985189
  • 669
  • 5
  • 16
  • 25
  • 2
    You should consider using some mature MVC framework with support for master pages (or page templates), where you can inject your banner in only one place. There are lots of options for PHP. Here is something: http://stackoverflow.com/questions/1933168/php-equivalent-of-master-page-in-asp-net – Anders Marzi Tornblad Mar 10 '15 at 15:12
  • One way or another you either need to manually update the pages with your banner code (usually a bad idea) or use some sort of include statement like what you've shown with PHP. – j08691 Mar 10 '15 at 15:13
  • You can instruct Apache to treat `.html` as PHP. – Funk Forty Niner Mar 10 '15 at 15:17
  • ^ did you read that? ^ – Funk Forty Niner Mar 10 '15 at 15:32
  • @Fred-ii- yes but from what I've read elsewhere on SO ppl are saying things like "why would you ever do that?" and such... makes me think it's bad practice and more of a hack than a real solution. – user1985189 Mar 10 '15 at 15:34
  • That's up to them and let them think what they want. It's your website, not theirs. How you want to run it is your business ;-) Instructing Apache to treat `.html` files as PHP is an option, and you can keep on using `.html` files as they are, even if there's no PHP inside them. – Funk Forty Niner Mar 10 '15 at 15:35
  • They probably say it's a bad practice because you don't need the PHP engine interpreting legitimate .html pages...minute but unnecessary overhead. Some people do it for "security through obscurity" so it's harder to tell that it's PHP code. I think it's pointless but some people like it. Personally, I consider it a bad practice if only because I had a few pages I did this with to avoid changing extensions...but a CPanel upgrade changed the PHP mime-type and my files started getting output as plain HTML, showing my source. – Kevin Nelson Mar 10 '15 at 16:00

2 Answers2

2

I don't think this is really best practice.

It's fine, especially for small sites.

I'd prefer to keep my basic webpages as html files and reserve php files for server-side logic - isn't that the way it's supposed to be??

Keeping business logic and view logic apart is a good idea, but you don't need to go so far as to ban .php extensions on files that don't include business logic.

You might want to investigate the MVC pattern and the approaches taken by frameworks such as CodeIgniter or CakePHP if you want to see how other people keep their logic and HTML separate.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • when you say it's fine especially for small sites, you mean small as in "few webpages" right? i.e it's not a scaling issue that could cause problems with higher web traffic or compromise SEO? – user1985189 Mar 10 '15 at 15:24
  • 1
    "few" is a YMMV thing. Search engines can't see how pages were generated, so it has no bearing on SEO. Includes are cheap, so it has no bearing on performance. All it matters for is ease of maintenance of your code. – Quentin Mar 10 '15 at 15:25
  • alright perfect. I will check some of these frameworks and if worst comes to worst just stick with php includes – user1985189 Mar 10 '15 at 15:36
-1

Either go ahead and convert the files to .php (totally fine practice), or use file_get_contents rather than include to get the HTML content:

<?php echo file_get_contents('banner.html'); ?>

Erik Johansson
  • 1,646
  • 1
  • 14
  • 19
  • 2
    You said "or use file_get_contents" as if he would not need to convert his HTML to PHP for that. If you have something in mind for how you would use file_get_contents in .html pages, that is not forthcoming. – Kevin Nelson Mar 10 '15 at 15:17
  • I meant that he would use that function instead of include. Answer edited to clarify. – Erik Johansson Mar 10 '15 at 15:26
  • 1
    Your edit doesn't clarify. It still says that `file_get_contents` is an alternative to using `.php` file extensions (which it isn't). – Quentin Mar 10 '15 at 15:27
  • Sorry, my interpretation was that OP is fine with using one PHP file to pull together the contents but that the include function would not accept HTML files (which is actually not the case). – Erik Johansson Mar 10 '15 at 15:35