scriptygoddess

25 Jan, 2003

easy includes for php

Posted by: Jennifer In: Bookmarks

I don't have much time to browse the MT forums – but had some "downtime" this afternoon to do so. There's often a lot of tricks for PHP in there. I found this thread, and towards the bottom, phideaux posted a neat trick to make using includes in your code easier.

The idea is you always use one directory for your includes. In your .htaccess file you add this line

php_value include_path ".:/home/user/public_html/includes"

Then any time you want to use an include in your file, you don't have to deal with typing out the big long absolute path (ie. /home/user/public_html/includes etc etc). All you need do is this:

<?php include("header.php"); ?>

Neat trick! :)

10 Responses to "easy includes for php"

1 | susan

January 25th, 2003 at 3:13 pm

Avatar

I work on .asp pages at work and we do that all the time. It save so much time when you need to find the include file. I never thought of doing that with PHP.. maybe I can skin my site yet!

2 | photomatt

January 25th, 2003 at 7:41 pm

Avatar

You can have it look in as many directories as you'd like as long as you seperate it with a ':'. It will look in the order you put things in. If you're going to use PEAR stuff make sure you include that as well.

3 | noel

January 25th, 2003 at 8:04 pm

Avatar

Or you could just do something like this…
include("$_SERVER[DOCUMENT_ROOT]/includes/includefile.php");

Works just as well with not as much work. Just a tip.

4 | cal

January 28th, 2003 at 12:22 pm

Avatar

that was brilliant! makes it all so much tidier

5 | Jester

January 28th, 2003 at 4:50 pm

Avatar

Also remember that a much more portable alternative is to use the ini_set() function to set the include_path in your script (say, do it in your database connection script or whatever). That way non-apache users (bless them) can use this method.

Using this method can also help you better categorise your includes, say you have /tutorials/, /code/, and /resouces/ you could create the same directories outside your document tree and have all the includes in the relevant directories then at the beginning of any script in /tutorials/ put:

ini_set('include_path', '/home/you/tutorials');

Saves messing around with full system paths or fighting with relative paths.

Btw, I found this site in our logs; I like it. Easy on the eyes, clean and to the point — how the internet should be.

-J

6 | sam

January 29th, 2003 at 7:20 pm

Avatar

Lol, I'm 'phidauex' on the boards. I'm happy this little hinty made it here :)

As to noel's comment, I find that having to remember to type the entire path, and environment variable each time is more difficult than just setting the path one place, and then not worrying about paths at ALL in the actual documents.

The ini_set() function is another handy solution, but remembering to call an additional function in each page would also negate part of the reason that I like putting the variable into .htaccess files. That reason being that I'm a terribly forgetful person who wants to do as little work as possible. If I can set the .htaccess file to include the paths I need, and then just include away without a care in the world, thats whats best for me!

The ini_set() function would probably be more appropriate for full php applications, then you could set a default search directory tree for that specific application using ini_set().

Peace,
sam

7 | Jester

February 1st, 2003 at 10:46 am

Avatar

Yep that's the point — portability. Non-apache users can't use .htaccess and you'll find alot of tutorials on the net are geared toward the apache server.

However, you'll find that most serious web hosts use apache.

You'll find PHP is full of functions that make it easy for the programmers but do cause unwanted overhead such as ob_start() which has become popular for people who get "headers already sent" errors when setting cookies and starting sessions. You should really try to structure your code so everything is done in the right order, as Perl, C/C++ CGI programmers have to. Keep the work your scripts have to do to a minimum.

8 | sam

February 1st, 2003 at 9:32 pm

Avatar

Oh, I appreciate the need to structure code. And portability is great for application level scripts, and its good to have multi-platform hints out there, but apache rocks the house, and thus its handy to have information on ways to use its powerful features to your benefit. You can run apache on nearly every OS right now, so its getting more common. Does IIS or any other major webservers have a feature similar to the .htaccess file?

Concerning ob_start(), and the need to structure code properly, I agree that people need to write good code to begin with, but I don't think PHP necessarily causes bad programming habits. I'm learning PHP coming from a background in C. Frankly, I was a bit suprised at how loosey-goosey it is in some ways, especially concerning the freewheeling manner in which you can initialize arrays and variables.

Fortunately there is quite a bit of good information in the PHP documentation about the speed of most of their functions. If there is a faster alternative, they do a good job of indicating it. Using explode() instead of ereg() for instance.. If people keep their eye on the documentation, I don't think its hard to produce reasonably efficient PHP scripts. You could do it with less overhead in C, but the lack of flexibility and handy functions would make the job much harder.

Peace,
sam

9 | Jester

February 5th, 2003 at 12:00 pm

Avatar

Well, I have to agree that apache does rock hehe, it's by far the best HTTPD out there and no other comes even close to it. I run it on windows 98 and, even on my bad oeprating system it seems pretty stable, I only remember it crashing once. .htaccess is a brilliant feature that's secure yet offers users alot of control over their environment.

You have to remember, though, that not everyone appreciates these features and you do get alot of silly people using other servers, writing tutorials just for apache is like writing a web page just for IE, most people use it sure, but you lose alot of your audience when you could have gotten them all by putting just that little bit of extra work in.

C is a whole different kettle of fish, I went from Perl to PHP to C and I found C very annoying at first. I was used to that freewheeling way of setting variables and arrays and defining them before hand was tiresome and long winded. Though, that's the power of C, it's much more efficient and you only use the variables you need, where as in a PHP script your environment is full of hundreds of variables and constants, most of which you'll never use anyways.

But for the amount of uneeded overhead PHP has, it's still very fast. C CGI will kick it's arse every time for speed, but for functionality, development time and learning curve PHP has the web scene flocking to it.

10 | KO

August 15th, 2003 at 6:52 am

Avatar

Does having two three php includes slow down the webpage? Or is it the same as including any other file, like an image, and doesn't matter?

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements