Help - Search - Members - Calendar
Full Version: PHP include
Forums > Resource Center > Webmasters' Corner
nitrogoat
Hi, as unprofessional and unsophisticated as it might seem i have always manually edited the source code of my web pages, going through page by page and copying and pasting new codes to replace old codes.

I'm pretty experienced with web design and was actually stunned that i had gone through such a tedious process without realising that what i was doing was a complete waste of time! I have just heard about the PHP include function which inserts coding from a file and into your web page.

Is it acceptable to use this function to insert large portions of coding from the header tag into pages, for example, menu coding and even some adsense coding from the body section of the page?

Thanks a lot guys!
libertie
Short answer: Yes! Actually, that's what it's used for. I typically put my header information, sidebar (if used), and footer all in their own text files and use php includes to have them easily show up on every page. Man, it would be a huge hassle to have to go into every single page and edit each file just to add one link to the sidebar.
Aberisk
I used to use php inclusions but now I use php dynamic inclusions which is like a iframe type of php inclusion so the loading time is cut into half and you dont even need to use any type of inclusion code into your files only your index.php and every page on your website gets converted :]
nitrogoat
Thanks very much for your replies, but i've got a couple of problems which i've been struggling with.

Firstly, when i use the php include and use my file name it won't open it up. All my relative paths work well and im experienced with these, for example all my pages will correctly load my stylesheet which is in the same location as my new .php file.

CODE
<?php include("/include.php"); ?>


I have resorted to using an absolute path to the file (below), which works, but i am intrigued as to why this coding itself won't work.

CODE
<?php include("http://www.site.com/include.php"); ?>


Also, as i convert several of my pages from .htm to .php i am finding that my google ads are becoming un-targeted. When i swap between viewing the .php equivalent and my original .htm the ads are completely un-targeted. Fustrating!

And so at the moment i have remained with my .htm files.
Any suggestions guys? I like trying to solve these problems myself, but they really are getting the better of me! And by the way, im using Notepad ++ as a php editor.
SteveZz
http://codeitall.com/PHP.php is a nice tutorial and explanation of why you'd want to use includes as well as how to go about using them. I hope it helps.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.