?
Solved

Getting a require function to look in the right place

Posted on 2005-04-06
5
Medium Priority
?
202 Views
Last Modified: 2006-11-18
All of my php files being with the following line of code:

require("/files/required.php");

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

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");
require("files/required.php");

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 php.net.  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:

require("/files/required.php");

Thanks for the help!
Alex
0
Comment
Question by:alexswebsite
5 Comments
 
LVL 5

Expert Comment

by:jericotolentino
ID: 13723421
Hi,

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

Accepted Solution

by:
_Marcel_ earned 1000 total points
ID: 13724799
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.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13724857
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.
0
 
LVL 3

Expert Comment

by:gerodim
ID: 13725414
At first try using (.) .
E.g.
require("./files/required.php");

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...
0
 

Author Comment

by:alexswebsite
ID: 13729753
Thanks for the help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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