Welcome to WebmasterWorld Guest from 54.167.75.28

Forum Moderators: coopster & jatar k

PHP includes

Best practices on what file type to include a PHP or html

     
2:54 am on Sep 21, 2017 (gmt 0)

New User from US 

Top Contributors Of The Month

joined:Sept 20, 2017
posts:29
votes: 0


I want to use PHP includes, for easy editing on repeated content. One file vs multiple. I've seen it show two was but confused. One way was to make a folder cut only the code you wanted to include in the spot cut from, place in editor and save as HTML in the created folder for the includes but I don't know if I'm supposed to give that HTML a doctype or just the cut code. Then on each page put the PHP includes code in were you cut the code from. I've tested it and it works both ways from what I could tell. When you look at the source code though, it looks right with only the code nothing else added to the include file, I also created a HTML file with all the usuals, doctype, head, title,body placed the cut code in the body an the page work just the same the source code was crazy though like a page in side a page. What's the affects of this?
3:51 am on Sept 21, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:10629
votes: 630


In your include file you do not need a doctype, a HEAD section, or body tags. That has the potential to confuse the user's browser... just put what you want to display on your page. Do use any necessary tags.

Includes are great. However, for most server set-ups, virtual includes* are less server-intensive and work faster than php includes.
 <!--#include virtual="/directory/filename.html" --> 

*However, if you are already parsing your pages for php code, the added php includes will likely not be much of an added issue.
4:17 am on Sept 21, 2017 (gmt 0)

New User from US 

Top Contributors Of The Month

joined:Sept 20, 2017
posts:29
votes: 0


So the code would look like this for the in include

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>include</title>
</head>
<body>
<header>my header code</header>

</body>
</html>


Above would be the html file to go in every page. If so, just like that, or leave out title, or add in meta like description and styles for the header that.

Below would be the page or pages with this header included. Of course I would have put the php code in and the include html file in a folder for other includes. is this correct?


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>page</title>
</head>
<body>
<!-- php code for header here -->
<article>page article</article>
<footer>page footer</footer
</body>
</html>

4:20 am on Sept 21, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:10629
votes: 630


When you say "header" what is in this include file?

If it's stuff that would normally be in the HEAD, then put your include code in the HEAD.

If it is stuff that normally would be in the BODY, then put your include code in the BODY.


[fix typo]

[edited by: keyplyr at 4:25 am (utc) on Sep 21, 2017]

4:24 am on Sept 21, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14426
votes: 576


Edit: I was typing while ALL of the above discussion was going on, so expect some overlap.

No, an html include doesn't get a doctype or, for that matter, its own elements like <head> and <body> -- unless, of course, the <head> itself is what you're including. The word "include" means exactly that: bring in the entire content of suchandsuch other document, and paste it in this location, just as if it were part of the original document. If a visitor to your site snoops and takes a look at the html source, there is no way for them to tell that some of the content was included from some other document. In this respect it's the same as a page created with php: the user sees only the generated html, not the original source. (If they do see the php code, something has gone terribly wrong.)

Look at your existing html pages. Are there parts that are the same in each one? Those are the parts that you cut out and put into includes. But the included parts will often be php rather than html, so you can do simple things like changing navigation depending on what page you're currently on. Most likely you'll have two includes to start with: one for recurring stuff at the top, another for recurring stuff at the bottom.

Note that if your pages are currently html, you can keep them that way and use SSI (Server Side Includes) instead. Yes, you can include php into an html document, so long as you're careful with your parameters. You just need to make sure includes are enabled for the html extension; this is much simpler than changing everything to .shtml.

:: detour to refresh memory ::

I currently have a php navigation banner at the top, paired with a straight-html footer (including invisible stuff like analytics code) at the bottom.
4:27 am on Sept 21, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14426
votes: 576


So the code would look like this for the in include

No, because a full header includes <title> and that's going to be different for every page. The only way it could be part of the include is if you used the page title as one parameter in php. That's possible in theory, but isn't the way people would ordinarily do it.
4:36 am on Sept 21, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:10629
votes: 630


@ztaco
This is all that should be in your include file:
<p>This content be included on the page, This content be included on the page, This content be included on the page, This content be included on the page</p>
note: I've put a paragraph opening and closing tag to illustrate that you can use tags.

This is on the target page (exactly as you said above)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>page</title>
</head>
<body>
<!-- php code for header here -->
<article>page article</article>
<footer>page footer</footer
</body>
</html>
4:53 am on Sept 21, 2017 (gmt 0)

New User from US 

Top Contributors Of The Month

joined:Sept 20, 2017
posts:29
votes: 0


The head of each page would be different, except maybe the meta for the author if its even important. I'm a newbie, but have learned a lot. from searches and some html, css online free classes. Even though I hated to read, couch potato, I really got into coding and currently do not do it for business but fun, the only site published is the one for my wife's law office. Its mobile friendly and works on most browsers I've checked on. Good results on google, other than from across the country were we can't help and call come in like we are local or it can be done online which it can't. The main thing that changes is the prices in a div. The header and footer for the page stay the same like the prices box except the prices go up which is only 2 places to change (on every page at the moment). When I have more time to design the site better, which I've done a lot in the first 2 years its been live. Now going on 4 and having more time I'd like an easier way to edit and redesign the site. I'm OK with html code and css but to come into errors with css, i use dw but mostly as a text editor and the instant view to see it. I used note pad in the past, took longer. We needed Adobe product for the business and the package came with it so what the hell, it's easy to upload, but over the past 4 years I still don't know how to use its full potential, but I like making my own code. Now I do use it for css help, for the extra browser support codes, but even on css, I have to code some my self, like using flex, and other code dw doesn't have or I haven't figured out to use it. I find things on google and searches, sometime not found in dw, new stuff not fully supported i guess, but do find fall back code if new code isn't supported with minimal flaws. The whole site is html now except for a form php that is call from any page which also has some of the includes to be used. what are your thought, for the old man newbie. Any reply would be awesome. Thanks
5:04 am on Sept 21, 2017 (gmt 0)

New User from US 

Top Contributors Of The Month

joined:Sept 20, 2017
posts:29
votes: 0


@keyplyr
That's what I figured, but wasn't for sure. Since the include is a html file, I wasn't sure if not declaring a doctype would confuse the browser. I'm really unsure of lots of the coding, and pares, parsing I don't even know what it is and it's been hard to find dumbed down explanations on certain things, that would make it easier to edit and work with my site. I'd like to create more, I love it for some reason, and make my fam mad talking and explaining what i've learned.
5:10 am on Sept 21, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:10629
votes: 630


Sounds good.

The newer DW builds are OK but the older versions added bloat to the code.

It's always best to code by hand. Cut'n Pasting code is OK but using tools to generate code is always a risk. Best to avoid it and use editors sparingly.

Tip: you will get more specific responses if you keep it as simple as you can, staying succinct and asking one question per thread.
12:50 pm on Sept 21, 2017 (gmt 0)

New User from US 

Top Contributors Of The Month

joined:Sept 20, 2017
posts:29
votes: 0


Thanks for the advice, first time forum user, long term looker of other posts with same issues.