Getting a require function to look in the right place

Posted on 2005-04-06
Last Modified: 2006-11-18
All of my php files being with the following line of code:


The file "required.php" initiates session variables, and sets up the database connection.  Location of this file is

I recently moved to a new host, however, and that line of code no longer looks in the right place for that file.

My webhost suggested the following code:

$PHPRC = getenv('PHPRC');
ini_set("include_path", $PHPRC . "/home/myaccount/public_html");

This works, but I wish I didn't have to change the code on all of my files.  Is there an easy way to update this site wide?  I feel like I should be able to edit my include directory or something like that in php.ini, but I'm not clear on whether I can just post a php.ini file into my account or exactly what code to change in the sample php.ini from  Also, how can I make this work with the code including the forward slash at the beginning of the location?  The way it was at my last host, this code would find the file regardless of relative location.  

Here's the code again that's at the top of my files:


Thanks for the help!
Question by:alexswebsite
    LVL 5

    Expert Comment


    If you happen to have another include or require statement near where you want to insert it, just place it there.
    LVL 8

    Accepted Solution

    You could of course create a .htaccess file in your document root with the following line of code:

    php_value auto_prepend_file /the/path/to/your/documentroot/setroot.php

    Then create a file called 'setroot.php' that changes your include_path (like suggested by your webhost):

    $PHPRC = getenv('PHPRC');
    ini_set("include_path", $PHPRC . "/home/myaccount/public_html");

    This script will automatically be run before all your scripts and it will fix your document root.
    LVL 25

    Expert Comment

    First of all check what your include path is set to. If it includes '.', then you should be able to just use require 'files/require.php', which will be a simple global search and replace. Using an absolute path for includes has its hazards, as you are finding.

    You can override a global php.ini setting in a .htaccess file:

    php_value include_path ".:/home/myaccount/public_html"

    Though that may break paths pointing to things like pear repositories.
    LVL 3

    Expert Comment

    At first try using (.) .

    If it doesn't work try using the whole path in the server....
    Print out a phpinfo() and find your directory on the server so u can write the full path...

    Author Comment

    Thanks for the help!

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now