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 HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
Get Relative Path to Website Root for Includes to Access from Anywhere

Last accessed pages

  1. CSS Outline (2655)
  2. Disable button and Enable it after specified time (17527)
  3. JavaScript Game - Find the Word (887)
  4. Ajax-PHP Chat Script (49473)
  5. Working with getElementsByTagName (13084)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (318)
  2. CSS cursor property - Custom Cursors (56)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  4. The Mastery of Love (41)
  5. CSS3 2D transforms (40)