Pulling My Hair Out Trying To Get PHP includes to Work!

Firstly just want to say thanks in advance.

I’ve just started converting a more or less static site into a php driven site. I haven’t previously
used php to any great length and I’m seriously having problems setting up includes. I’m working
on localhost at the moment.

My directory/project structure is as the following:


mysite.com/index.php

mysite.com/includes/

mysite.com/category-1/demo.php

mysite.com/category-2/

mysite.com/category-3/

...etc

If at all possible, what I’m trying to do is set the path to the includes via htaccess and then be
able to use that path on any page, in any directory.

I’ve currently got this in my htaccess file in the root directory:


php_value include_path "php"

The implementation below then works a charm when called from files within the root directory.


<?php require_once("some-include.php"); ?>

or


<?php include_once("some-include.php"); ?>

It fails however when includes called in the same manner with files in subdirectories. In order to
get it to work I’ve had to create a separate .htaccess file for each directory and place the following:


php_value include_path "../php"

How can I set the path globally in my main htaccess or php.ini and be able to just implement the
includes without having to use relative paths?

Any suggestions are greatly appreciated at this point.

In my case I have full access to the server and I created a folder in the php directory; the path to this folder is in my php.ini file.
I can include any file I want from any page on the server with a simple include e.g. include (‘layout.css’); and not need to worry about any paths.

Can you do it this way or do you not have enough access?

I belive php_value include_path “php” is the path to the php folder that is above the server root and I would think you need to define the path to you includes folder in greater detail.

It is because you are using relative paths. Use absolute.
[COLOR=#464646]php_value include_path "/start/from/the/root/use/the/full/path/to/php"[/COLOR]

Awesome, thanks for your suggestions @logic and @Rubble.
I’ll implement them and and see how it goes, my brain’s fried
right now.

Much appreciated.

@RogueSkolar

Try this before setting your links:



echo getcwd(); die;

# now set the relative path wherever _file_001.php is stored
 #include_path "_file-001.php"             // this directory
 #include_path "../_file-001.php"          // up one  directory
 #include_path "../../_file-001.php"       // up two  directory
 #include_path "../../../_file-001.php"   // up to wherever directory


Hi RogueSkolar. Welcome to the forums. :slight_smile:

Does that mean you have the site running locally, on your computer? Normally, the paths will be different on your local computer from the paths on the remote server, which can be a pain. So you can set up your hosts file to treat your site like it’s online, meaning that you don’t have to change any code when you put your site on the www. If you are doing this locally, what system are you on? (Mac, PC?)

Good point @ralph_m ;, it made me read the OP again.

Going back to the OP, you initially said this was your setup.


mysite.com/index.php

mysite.com/includes/

mysite.com/category-1/demo.php

mysite.com/category-2/

mysite.com/category-3/

...etc

There is something I need to warn you about – that is not how you would normally set things up, an includes directory inside your public website directory structure.

Include folders normally contain code which you do not want made public so you would normally have REAL (HDD) paths like this:


/var/www/mysite.com/index.php

/var/www/mysite.com/category-1/demo.php

/var/www/mysite.com/category-2/

/var/www/mysite.com/category-3/

(which of course appear as mysite.com/index.php etc once your server is configured correctly, much as @ralph_m; is suggesting)

Then here is the shared place you would keep all of your include files:


/var/www/includes

Which would then be available to all of your websites.

(I am using /var/www/ as the default web folder, yours may look more like /xamp/public/http-docs/ or something … it matters not)

Also, there is nothing stopping you have a folder WITHIN that /includes folder for just mysite.com

Then you can set the ini directive to tell PHP that all include files live at


php_value include_path "/var/www/includes"

This makes life really simple, all include files will initially be searched for in this directory.

Tradition would then have it that you would have say a file with all of your database connection credentials in a file for this project and name it:


mysite-config.php

and save it as:

/var/www/includes/mysite-config.php

You then include that file FROM ANYWHERE in your website PHP files with the simple instruction:


include 'mysite-config.php';

Another example might be a really useful piece of PHP code which outputs variables for a date picker. You might use that in more than one project so you’d save it as /var/www/includes/date-picker.php and then from all your projects on different websites on this server you’d simple do:


include 'date-picker.php';

You want to totally avoid mysite-config.php being accessible from your website, by someone stumbling across it in a publicly accessible folder such as mysite.com/includes/mysite-config.com

Personally, I prefer to set the include_path directive directly in the php.ini file, you will be able to do the same on your localhost server, but you’d need to check with your hoster if you get to have your own ini file, or if they are willing to set this variable up in your apache conf file for you.

WOW thanks a bunch guys, I’m much obliged.

@John_Betong

Thanks for your solution. I’m sure to play around with this as I drill down on the perfect or at least,
least cumbersome implementation. Have you got a link where I can find out what these statements
are doing in particular echo getcwd(); die;. Thanks.

@ralph.m Thanks for the welcome :cool:

Yes these are all concerns that have not escaped me. It’s one of the first things that came into consideration,
well actually when I started running into problems!

I’m running Ubuntu. I’ve got Apache, MySQL and PHP 5 set up on my local (development) environment.

The root/path for localhost on my machine is: /var/www/

The path to my web project on localhost is: /var/www/mysite.com/

This is a screen grab of the directory structure on my webhost a la’ the usual cPanel:

My thinking is that:

/home/username/ on webhost = /var/www/ on localhost.

and

/home/username/public_html/ on webhost = /var/www/mysite.com/ on localhost.

So now my problem lies in properly defining a directory and path for includes on localhost that will match
that on my webhost’s server which brings me to the points @Cups has raised.

@Cups, thanks for your insights.

The php.ini file in public_html/ has the following path set for includes.

; UNIX: “/path1:/path2”
include_path = “.:/usr/lib/php:/usr/local/lib/php”

I presume this structure is what you are referring to – that is to not place includes in public_html/
where it is publicly accessible. I’ve created a new directory for includes and placed it at the same level
with this php directory:

If I’ve understood this correctly, the absolute path for this will be:

/home/username/includes/ which can be mapped to /var/www/includes/ on localhost.

Then what I’d need to do is just change the include path in php.ini to point to this new directory and possibly
copy over some configuration files in the original php directory (if needed) and all would be ready to go and I
could simply sync the include directories when I’m ready to deploy?

I’ll play around with this and hopefully get it to work, but I’ve got a lot of self education ahead I think…

Big ups to all.

Hey man, big props for taking the time to create such a long reply – and to so many of us. :slight_smile: with screen shots and all!

I will let you play with your current config and lets hope you get yourself sorted.

@RogueSkolar

@John_Betong

Thanks for your solution. I’m sure to play around with this as I drill down on the perfect or at least,
least cumbersome implementation. Have you got a link where I can find out what these statements
are doing in particular echo getcwd(); die;. Thanks.

Details of echo, getcwd() and die() functions, etc can be found by searching:

http://php.net/

I find that the site is an excellent resource when developing and well worth the effort of becoming familiar with the layout.

As somebody previously mentioned “links are a pain”. The getcwd() function returns the path of the file the browser requested. The include function can be either an absolute or relative path. Using the script displays the path then halts execution. Once you know where you are then it us up to you to choose the path of the proposed included file and/or image.