Viewed   68 times

Just to confirm, is using:


the same as using: /

in HTML.

Eg. If current document is:


I could use (in HTML) to start at the roort:


and to do the same in PHP I would have to use:

$_SERVER["DOCUMENT_ROOT"] . "/somedoc.html";

Is that correct? Is there an easier way to do it?


<a href="<?php echo $_SERVER['DOCUMENT_ROOT'].'/hello.html'; ?>">go with php</a>
    <br />
<a href="/hello.html">go to with html</a>

Try this yourself and find that they are not exactly the same.

$_SERVER['DOCUMENT_ROOT'] renders an actual file path (on my computer running as it's own server, C:/wamp/www/

HTML's / renders the root of the server url, in my case, localhost/

But C:/wamp/www/hello.html and localhost/hello.html are in fact the same file

Sunday, August 14, 2022
<form name="add" method="post">
     <select name="age">
        <option value="1_sre">23</option>
        <option value="2_sam">24</option>
        <option value="5_john">25</option>
     <input type="submit" name="submit"/>

You will have the selected value in $_POST['age'], e.g. 1_sre. Then you will be able to split the value and get the 'stud_name'.

$stud = explode("_",$_POST['age']);
$stud_id = $stud[0];
$stud_name = $stud[1];
Tuesday, October 4, 2022

The usual workflow:

  1. Provide a Javascript rich-text editor for your users such as TinyMCE:
  2. Grab the source generated by the RTE and filter it through HTML Purifier before saving to the database.
  3. Escape the existing HTML: <div id="myHtml" style="display: none"><?php echo htmlentities($html); ?></div>
  4. Re-populate the RTE via Javascript - in the case of TinyMCE as follows: tinyMCE.activeEditor.setContent($('#myHtml').html());

You can also load the HTML content via AJAX.

Tuesday, December 27, 2022

Anything involving realpath() and DOCUMENT_ROOT is going to fail hard when the server's got aliases configured. Consider a scenario where Apache's got a configuration like this:

DocumentRoot /home/httpd/html
Alias /testalias /home/otherdir

And you access a script at

The script will return:

realpath(dirname(__FILE__)) -> /home/otherdir
$_SERVER['DOCUMENT_ROOT'] -> /home/httpd/html
BASE_DIR -> /home/otherdir
BASE_URL -> /home/otherdir/

and yet the rest of the site actually exists in /home/httpd/html

You might have better luck reconstructing the URL based on $_SERVER['SCRIPT_NAME'], which is the path/script name portion of the URL:

$_SERVER['SCRIPT_NAME'] -> /testalias/script.php
Friday, October 7, 2022

What I've done in the past is to use a setup like this:

define('DIR_BASE',     dirname( __FILE__ ).'/'); 
define('DIR_SYSTEM',   DIR_BASE.'app/');

This would be placed in a file root of your project, which will give you access to other areas of your file structure relative to the root.

For your case, it would look like this:

define('DIR_BASE',     dirname( __FILE__ ).'/');
define('DIR_CSS',      DIR_BASE.'css/');
define('DIR_PHP',      DIR_BASE.'php/');
define('DIR_CONTENT'   DIR_BASE.'content/');

This would go in getpath.php in your root, and would give you the ability to reference any file or directory regardless of where it is placed in the directory structure (be sure to include it wherever you'll be using them). You wouldn't need to change your directory structure or anything like that, and you don't have to worry about any vulnerabilities with something like this, since it's internal.

Edit I keep coming back to the structure of the system. Is this a multi-site system that uses the same CSS through out? If not, then what is the justification for having the directory structure laid out like this? Can you give just a little more detail please?

Tuesday, October 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :