tomccabe Posted April 14, 2010 Share Posted April 14, 2010 I'm trying to include (or more accurately require_once) my database config file that is above the public folder on my server. I've read a lot of posts on people doing this but none of it seems to work for me. Each of my pages that require a DB connection call an include to my connection.php file which looks like this: <?php require_once 'config.php'; $connection = mysql_connect($db_hostname, $db_username, $db_password); if (!$connection) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database, $connection) or die("Unable to select database: " . mysql_error()); ?> That config file, which contains the actual login info is what I would like to grab from above the public area (right now it is in the same folder). If I echo out $_SERVER['DOCUMENT_ROOT'] on my shop's index page I get the following: /services/webpages/g/r/mysite.com/public/shop Where I've dropped a copy of my config.php file is (following this structure, though the "include" folder is at the top level of where I can access via FTP): /services/webpages/g/r/include/ I can't seem to call to this file from the many places I've looked at this. Among others, I've tried: require_once '/services/webpages/g/r/include/config.php'; to no avail. Does an absolute path not work with require? Is my path incorrect? Quote Link to comment https://forums.phpfreaks.com/topic/198505-absolute-path-include/ Share on other sites More sharing options...
aeroswat Posted April 14, 2010 Share Posted April 14, 2010 why not use something like include ../../config.php? Quote Link to comment https://forums.phpfreaks.com/topic/198505-absolute-path-include/#findComment-1041612 Share on other sites More sharing options...
tomccabe Posted April 14, 2010 Author Share Posted April 14, 2010 I tried that and I couldn't get it to work. And I think I figured out why. I found this post http://roshanbh.com.np/2008/01/absolute-path-and-relative-path-file-inclusion-in-php.html and trying that method I was given an entirely different directory tree above the public folder. I think why your method didn't work is because I was trying to reverse through the wrong structure. I guess I need to figure out why these are giving entirely different directories: echo $_SERVER['DOCUMENT_ROOT']; echo dirname(__FILE__); I tried to reverse through again as you suggested with no luck. I CAN use the absolute path. My problem with that is that is goes something like: /services/webpages/util/2/r/CLIENTS-FTP-USERNAME.site.HOSTNAME/includes/config.php which gives away a whole lot of information, exactly the kind I am trying to protect. :'( Quote Link to comment https://forums.phpfreaks.com/topic/198505-absolute-path-include/#findComment-1041622 Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2010 Share Posted April 14, 2010 What php errors do you get when you try this with full error_reporting/display_errors turned on? Add the following two lines of code immediately after your first opening <?php tag, before the include/require statement - ini_set("display_errors", "1"); error_reporting(E_ALL); If you are on shared web hosting, the folder closest to the root that you should be able to access is /services/webpages/g/r/mysite.com/. That you can navigate closer to the root of the folder tree using FTP likely indicates that the permissions of the FTP server are not setup correctly. You should be attempting to do this in /services/webpages/g/r/mysite.com/include/ Did you use your hosting control panel to create the include folder or your FTP client? You should use your hosting control panel so that the folder will (should) have correct permissions and ownership so that the web server/php can access it when your web pages are requested. Quote Link to comment https://forums.phpfreaks.com/topic/198505-absolute-path-include/#findComment-1041632 Share on other sites More sharing options...
tomccabe Posted April 14, 2010 Author Share Posted April 14, 2010 Well, all of this lead me to the solution. On this server the client can host more than one site. I was trying to put the file above that site's folder and I just used a couple ../ Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/198505-absolute-path-include/#findComment-1041668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.