0

I created initialize.php to define my project's root directory.

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', DS.'works'.DS.'myproject');

I include it with something like this from any directory.

require_once("../includes/initialize.php");

and I include other files easier. I am going to upload the files to server, then I should only change the initialize.php file.

It works. But, when I use user friendly URL's, and call ajax files from it, I don't know my directory hierarchy to include initialize.php.

I don't want to do this:

require_once("/works/myproject/includes/initialize.php");

for every file. Because it is going to change when I upload everytime.

I don't want to use session or cookie for everyuser.

There should be a trick for this but I couldn't find. Is there a way for getting project root from anywhere while using user friendly URL's and ajax calls?


I fixed it.

when I call it with ajax it's ok. But I included it as php too for some conditions.

Because of current and the ajax files are in different folders, it crashed.

So, when I change it to only require initialize.php when called with ajax, problem solved.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
potan
  • 63
  • 1
  • 3
  • 13
  • Please post your fix as answer and then mark it as answered, so that other can see this question has been completed. Then remove the [fixed] from the question title. – Spontifixus Sep 28 '12 at 20:20

3 Answers3

1

If you're using Apache, you could try adding your includes directory to your include_path directive.

Find the current value by doing a phpinfo() and look for include_path and then try re-declaring it in your top-level web directory and add the absolute path to your includes directory.

So if the default value is:

include_path = ".:/usr/bin/php:/some/other/directory"

just copy-paste the same thing in your .htaccess file but add your include directory on the end, separating it with a colon.

include_path = ".:/usr/bin/php:/some/other/directory:/works/myproject/includes"

and that way you shouldn't have to reference the absolute path every time.

This all depends on the permissions your web host gives you. There are other (easier) ways to do this, but I find that most of them are usually restricted by hosting providers, and manually setting it via .htaccess is usually the most dependable way to get this done.

Here's a page listing a few different ways: Five ways to create include path for PHP

nageeb
  • 2,002
  • 1
  • 13
  • 25
0

Simply doing this:

require_once("../includes/initialize.php");

is enough because PHP doesn't look at your friendly URLs. It includes whatever you give him to include relative to your script.

Also there is $_SERVER['DOCUMENT_ROOT'] that will give you an absolute path from your root directory.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

It is a good approach to define all your directories in a common file as you added initialize.php. You will have to include this common file in every file on the project. User friendly url's have no effect on the file inclusion.

Tariq Aziz
  • 788
  • 3
  • 13
  • 29