Jump to content

Recommended Posts

Right, here goes my first article, critics welcome!

 

Php Includes

PHP includes are proberley the best time saving piece of coding I have ever come accros. They give you the ability to change all your files on your website, just be editing a single page, so you instantly save yourself time and hassle, and whats more, there so simple, you wonder how you ever did without them!

 

What are Php Includes Code Wise?

PHP includes are no more then a simple line of code in your original page. E.g.:

><?php include("Pool/Header.php"); ?>

This single line tells the server to also "include" the file mentioned in the page before it gets sent off to the user's browser. The user sees nothing different, because to them, its a single page.

 

What are good are PHP includes really then?

Ah, the golden question, well here goes. Do you have a constant "menu" bar at the side of the page, or a banner, header etc which always occurs on all the pages. Maybe the bottom stating contact details and copyright notices. All of these are examples of coding which appears page after page after page, which you may want to edit at any point. But wait, that would mean editing, say 100 pages! By using the PHP include feature, you just create one file for say, the banner heading, and every page, just enter one line of code to call it. Then if you make any changes to the banner heading, you just change the one file, which will instantly change all other pages!

 

How do they work?

Basically, when a user requests any page from the server, the server grabs the page, and runs through it. As it runs through it, it will excute any commands it comes across. When using PHP files, anything in a <?php command ?> will be acted upon. Once its been acted upon, the server then sends the user the page. This takes place nearly instanteously, so the user never sees any diffenrece.

 

How do I use Php includes in my website?

Very easily! Here is my golden pointer:

 

Your page must be saved as a PHP file. It wont work if you dont!

 

Right once we got that sorted, here are some examples. I have produced 2 includes, one for the header, and one for the footer, and the index page.

 

Include header - saved as "header.php"

><html>
<head>
<title>My Brand New Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" CONTENT="Jim Gurus Website">
<meta name="keywords" content="jim, guru, totalchoice, hosting">
</head>
<body>
<img src="Bigbanner.jpg">

 

Include Footer - Saved as "footer.php"

><div align="center" class="text">
<font face="Times New Roman, Times, serif">
<img src="flag.jpg" width="79" height="75"> 
<b>© 2003 Proudly Produced By
<a href="mailto:webmaster@jimgurusdomainname.com">Jim Guru</a>
</b>
</font>
</div
</body>
</html>

 

Right so there are the two includes, which I can now include on every page. My two pages calling these includes would look like:

 

Index Page- Saved as "Index.php"

><?php include("Pool/Header.php"); ?>

My Front page , my website may not be big right now, but come back soon!

<?php include("Pool/Footer.php"); ?>

 

What the user then sees is:

 

><html>
<head>
<title>My Brand New Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" CONTENT="Jim Gurus Website">
<meta name="keywords" content="jim, guru, totalchoice, hosting">
</head>
<body>
<img src="Bigbanner.jpg">
My Front page , my website may not be big right now, but come back soon!

<div align="center" class="text">
<font face="Times New Roman, Times, serif">
<img src="flag.jpg" width="79" height="75"> 
<b>© 2003 Proudly Produced By
<a href="mailto:webmaster@jimgurusdomainname.com">Jim Guru</a>
</b>
</font>
</div
</body>
</html>

 

So now I have 2 files, which i can edit, and will change all my pages on the website. Maybe I would want to change the banner, or the meta tags, and rather then changing all the pages, i just change one!

 

Other tricks with includes

Page Variables

You can pass variables on, e.g. page titles:

><?php $page_title="Jim Gurul"; include('header.php'); ?>

where the header is:

><html>
<head>
<title>
<?php echo $page_title; ?>
</title>

 

Paths

You can put all your includes in a folder, like I do. I call mine "pool":

><?php include('../pool/header.php'); ?>

 

Includes inside includes

I am not going to show you an example here, but any bit of a coding you are including can include another include, and so forth. Although this may sound really complex its not!. Promise

 

Overall

Its difficult to comprehend how usefull these includes are, but thousands of people are using these nifty commands to save them time and effort and I urge you all to give them a try, as you will proberley wonder how you ever managed without them!

 

Jim

Link to post
Share on other sites

Holy deja vu, Batman... I think I've seen this before.

 

Just giving you a little grief, Jimuni. This is a well written tutorial that explains includes in a bit more detail than I did. And I think that the subject is definitely important enough to warrant two threads on it.

 

Just to give the rest of TCH members an idea of how useful this concept is... I have a multi thousand page site that I manage.

 

I can change two files, upload them to the server... and my whole site changes instantly... layout, css, whatever I want.

 

Neat trick here!

 

You can avoid ever serving up another page that has the ugly, unprofessional title "Untitled Document" just by putting in a little 'if' loop that says "If there's no $title variable declared, then $title is 'Ultimate Offshore Gambling' ... or whatever your want your default title to be.

 

The other way to do it... which I use. Is to put a default title followed by the $title variable. So there's always a title, but I can put more details in the title if I want.

 

Here's an example

<title>Stamp Collecting: <?php print("$more_title"); ?></title>

 

Worst case... The title is "Stamp Collecting:"

 

If I add more info it could be "Stamp Collecting: Rare Foreign Stamps"

 

Hope that helps.

Link to post
Share on other sites

Don't worry about it for a second... I don't mind... I'm glad you posted.

 

I'm not the only one who can write about PHP. No one ever asked me to in the first place.

 

Besides, I'm glad to have the help. I have stuff to get back to, pronto.

 

I was giving you kudos that you did a great job and expounded on the idea and the details.

 

Well done.

 

If you and I keep it up, we'll write our own PHP manual in no time.

 

Includes can save so much time for so many people that if they just 'get' this one thing... and nothing else that PHP has to offer... time well spent.

 

So it deserves it's own tutorial... which it now has.

 

Good job.

Link to post
Share on other sites

Only other thing I'd say... is that my past experience with including header and footer taught me that you have to take into consideration that a url to your header file might be correct in one file, but totally incorrect in another file in another directory.

 

One solution (of many) is this:

 

<?php

include $_SERVER['DOCUMENT_ROOT']."/inc/header.php";

?>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...