include advice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrgooding
Forum Newbie
Posts: 23
Joined: Thu May 22, 2008 11:45 am

include advice

Post by mrgooding »

Hi all

I've done a bit of research on this, but couldn't find anything definitive. I've started constructing a website, with two separate pages that I include at the top of most of my .php pages - one is for authentication, the other is my database connection function.

Do I need to ensure each page includes the two files, or does including them on an index page which links to others allow them to cascade, so to speak?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: include advice

Post by pickle »

No - you need to include them at the top of each page. When viewing a page, the web server has no idea what other pages you've viewed in the past - so you need to make each page function fully on its own.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: include advice

Post by JAB Creations »

I have an extensive includes template setup.

First I have the parent templates which include child templates.

If you request a file such as main.php it will include only parent templates (and a single different type of template where the unique page's content is pulled from).

When you look at the parent templates they include the child template files. On my site I allow lots of various cookie based tweaks such as choosing a DTD, media type, CSS themes, initial keyboard focus, etc. So to keep everything organized I have a numerical top to bottom numbering system. To give you an idea it sort of looks like this...

template-child-01-xml.php
template-child-02-dtd.php
template-child-03-doctype.php
template-child-04-css.php
template-child-05-javascript.php
template-child-06-body.php
template-child-07-sidebar-00.php
template-child-07-sidebar-01-search.php
template-child-07-sidebar-02-signin.php
template-child-07-sidebar-03-tools.php
template-child-08-menu-01.php
template-child-09-menu-02.php


When I first started using this form of organization it only took me a couple of months to finally settle on the final naming scheme though it is vastly easy for me to figure out what file I need to look at even if I don't have the exact name in my head. You'll notice the first child 07 sidebar which is like a third level includes. So the includes order from the requested file to the search would be main.php --> parent parent --> template-child-07-sidebar-00.php --> template-child-07-sidebar-01-search.php.

Of course this might not work for everyone nor for all situations though it's an option you can keep in mind if you find it may better serve a future project you choose to pursue. Have fun! :mrgreen:
Post Reply