Php-mysql Course

If you have files in different directories that include a php file which also includes other file, a problem can be the patch that can changes when the file is included from different level of directories.
For example, in this files and folders structure:
/
- dir1/
  - dir1_2/
      level_2.php

  level_1.php

config.php
index.php
- "level_1.php" (which is in "dir1/") include "config.php".
- "level_2.php" (which is in "dir1_2/") include "level_1.php".
- "index.php" (in root "/") include "level_1.php".

As you can notice, "level_1.php" is included from root (in "index") and from "dir1_2/" (in "level_2").
If in 'level_1.php' we include "config.php" using:
include('../config.php');
This code will not work when "level_1.php" is included in the other files.

The problem is, how to include "config.php" in "level_1.php" so this file can be accessed from anywhere?
I tried with absolute /full server path, using:   dirname(__FILE__) (or: __DIR__ ), but didn't work in Windows.
After searching on the net to find a proper solution to this problem, I found the following code, that gets the relative path to Web Site Root directory ("www/", "public_html/", "httpdocs/"):
$current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
$current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
$the_depth = substr_count( $current_path , '/');

// Set path to root for includes to access from anywhere
if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
else $pathtoroot = str_repeat ('../' , $the_depth);

echo $pathtoroot;

I put this code into a function that returns the relative path to Web Site Root directory from any current folder, and the problem was solved using this function to include "config.php" in "level_1.php", that can be accessed from anywhere.
// returns the relative path from current folder to Web Site Root directory
function getRootPath() {
  $current_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
  $current_host = pathinfo($_SERVER['REMOTE_ADDR'], PATHINFO_BASENAME);
  $the_depth = substr_count( $current_path , '/');

  // Set path to root for includes to access from anywhere
  if($current_host == '127.0.0.1') $pathtoroot = str_repeat('../' , $the_depth-1);
  else $pathtoroot = str_repeat ('../' , $the_depth);

  return $pathtoroot;
}

// include file located in root
include(getRootPath(). 'config.php');

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
Get Relative Path to Website Root for Includes to Access from Anywhere

Last accessed pages

  1. Accessing form fields (1828)
  2. Node.js with MySQL Database (912)
  3. Forms and Input (3648)
  4. Cookies (853)
  5. dompdf (3862)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (496)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (91)
  3. Read Excel file data in PHP - PhpExcelReader (55)
  4. The Mastery of Love (43)
  5. The Fifth Agreement (42)