0

I currently learning HTML, JS and PHP all at the same time (cus why not?)

And implementing the header.html in PHP is not so fun.

It works on the main page or "index.php" by just adding:

<?php include("header.html");?>ex.php

And then the other pages by adding:

<?php include("../header.html");?>ex.php

But after a while you forget how many "../../../" you need to put in, in order to get to root of the server folder. Is there a way to get to root of webserver easily? simply "/header.html" is not working out.

any ideas?

Solution?:

My current solution is simple to set the header path as

/home/yuannan/Web/header.html

Or where your file is from the ROOT OF THE SERVER and not the web server

  • what happens if you just try `include("header.html");` just like in index.php? – Lal Jul 19 '15 at 18:10
  • header doesn't appear, as i guess the server is trying to look for header.php within it's current folder. which could be miles deep from with the root folder as my website has everything from memes to rant to even help with basic BASH. This is why "../../../" will be massive problem for me as most pages are within a deep nested folder and will be a headache to keep track –  Jul 19 '15 at 18:14
  • 1. it is not really possible to reliably find the "root" folder of a web application. 2. this is typically solved by a central configuration 3. using typical MVC frameworks this question does not really arise that much. – arkascha Jul 19 '15 at 18:17
  • if that is the case, i would suggest you to declare a global variable with the base address as its value and then call the required file as for eg, `` where `base_href` will have the root folder – Lal Jul 19 '15 at 18:17
  • and where would I declare that? –  Jul 19 '15 at 18:20
  • see if [this](http://stackoverflow.com/a/13530536/3168859) helps – Lal Jul 19 '15 at 18:25
  • hmmmm, I'll master HTML first and then move onto PHP. Guess this is too much for my fragile young brain. :/ –  Jul 19 '15 at 18:30

2 Answers2

0

What I usually do is:

  1. Use a standard location for my php "library" files (lets say [project_path]/lib/php/)
  2. Create a helper file "setpath.php" which is going to set the correct PHP path(s). In this file I work with relevant paths which are webroot independent:

    function setPath() {
        // get the path to the root folder of the 
        // project using our current known location
        $bn = dirname(dirname(dirname(__FILE__)));
    
        // Get absolute path
        $bn=realpath($bn)."/";
    
        // Extend with all needed paths (few examples)
        $new_paths = $bn.PATH_SEPARATOR."$bn/db".PATH_SEPARATOR."$bn/templates";
    
        // Set the environment
        set_include_path(get_include_path() . PATH_SEPARATOR . $new_paths);
    }
    
  3. Now each file needs to know how to include this and call setPath(), so from your index this is include("lib/php/setpath.php") and from your lib/php/db this would be include("../setpath.php").

  4. You are now good to include any file which is in the registered paths without knowing its excact location

Additionally, I have found out that using Autoloading can save you lots of effort trying to figure out dependencies manually...

Finally, in some cases knowing the server webroot is needed. In these cases I use something like the following (also in my lib/php/ folder) which provides the file-system path and the webroot of the project as GLOBAL variables (... some may say bad practice ...)

// Fix document root to work with mod_alias (that maybe buggy)
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);

// The root of the project in the Document 
$SRV['BASEDIR'] = substr( __FILE__,0, strrpos(__FILE__, "/lib"));
$SRV['WEBROOT'] = substr( __FILE__,0, strrpos(__FILE__, "/lib"));
$SRV['WEBROOT'] = str_replace($_SERVER['DOCUMENT_ROOT'],"",$SRV['WEBROOT']);

I use the above tools along with some Autoloaders as the base of my projects. Hope that helps you or inspires someone to provide a better solution.

Andreas

urban
  • 5,392
  • 3
  • 19
  • 45
0

What I have done for my website is simply set a general filepath and then use that throughout the website just below the <body> tag. Pretty simple and straightforward. Just make sure your $Filepath points to wherever you have your header file stored, it'll mitigate the issue of having to use backticks for each file layer. Like so:

    <?php
        {
        $Filepath= "c:/wamp/www/";
        include ("{$Filepath}header.php");
        }
    ?>
NMaduro
  • 21
  • 1
  • 1
  • 12
  • I'm on Ubuntu Apache 2.4.7 so will I have to set it to $Filepath="/home/yuannan/Web" or where my files are from the "root" of the machine and not the server? –  Jul 20 '15 at 08:16
  • It should be the file path of where the files are located on the server. So it should be $filepath="/home/yuannan/web". If not, try the path from the root of the machine. – NMaduro Jul 21 '15 at 17:55
  • I'm on windows using WAMP so it may be slightly different. – NMaduro Jul 21 '15 at 17:55