Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/198505-absolute-path-include/
Share on other sites

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.  :'(

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.