Automatically include a path in php

This is a quick tutorial on how to automatically include a path in every php file that you create. This does not mean that you no longer need to do an include:

include_once('class.string.php');

I just cleared things up. Since that was my assumption when I first learned that I can explicitly include paths in every php file that I create without actually including it.

Things you’ll need
Wampserver
Text editor

Things to do
First you have to launch wampserver, then right left click on the tray icon >php>php.ini

image

Open the file using a text-editor if windows prompts you what program should you use to open the file.

After the file has been opened. Press ctrl + H on your keyboard then type include_path, click on find next until you see the windows word right above the word include_path.

Uncomment the line by removing the semicolon before it:

; Windows: "path1;path2"
include_path = ".;C:php_includeshome_made"

If you want to include multiple paths then you can separate them using a semicolon, like the example that has already been given just above the include path.

The path that I included contains php classes that I could use for login, connecting to mysql database, sessions, and formatting strings and dates:

image

You can also include files which you will include as a header, footer or a sidebar. But be sure to reference the css and javascript files needed by those files which you have put in the include path.
Save the file and restart all services from the wampserver tray icon.

image

All you have to do now is to include those files inside the files where you need them:

require_once('class.sessions.php');

It’s a bit easier when you include it using the full path isn’t it?
Don’t forget to declare an object of the class if it’s a class that you are trying to include.

$sessions = new sessions();

 

Conclusion

That’s it for this quick tutorial. Just make sure that the paths that you included exists, and that the files that you have included exists in the included path. Because you will get a nasty error on every page if the path doesn’t exist.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s