MySQL Forums
Forum List  »  PHP

Re: Need help connecting MySQL with PHP
Posted by: Peter Brawley
Date: February 26, 2013 12:34AM

> I don't know that you can "include" a file from a folder above the htdocs folder.

You can, indeed it's necessary for hiding connection details. Here's one way to do it, assuming you develop under Apache on an offsite devel machine, that you deploy on a standard *Nix hosting server, and of course that your code has to run seamlessly in both environments:

1. On your local server create the folder apache/includes for your include files. On your hosting server, create the includes folder in the folder just below public_html.

2. Assuming your include file is called myincfile.php, start your web pages with code like this ...

$host = ( isset( $_SERVER['HTTP_HOST'] )) ? $_SERVER['HTTP_HOST'] : "localhost";
if( $host=='localhost' ) {
  $incfile = "c:/apache/includes/myincfile.php";
}
else {
  $docroot = $_SERVER["DOCUMENT_ROOT"];
  $home = substr( $docroot, 0, stripos( $docroot, "/public_html" ));
  $incfile = "$home/includes/myincfile.php";
}
require_once( $incfile );

Options: ReplyQuote


Subject
Written By
Posted
Re: Need help connecting MySQL with PHP
February 26, 2013 12:34AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.