I am using Apache server for PHP. How can I retrieve my web root in PHP, like http://localhost/testthesis/
?
For your webservers root directory, use:
$folder = '/';
For the directory of the retrieved script, use:
$folder = './';
For your webservers root directory, use:
$protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
$folder = $protocol . $_SERVER['HTTP_HOST'];
For the directory of the retrieved script, use:
$protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
$folder = $protocol . $_SERVER['HTTP_HOST'] . '/' . basename($_SERVER['REQUEST_URI']);
Web root is always just /
. You'd never need the hostname or protocol part, and root can be only root of the server, not some folder or file.
If you need some path, like /testthesis/
- there are ways, but it has nothing common with web root.
If you need a filesystem directory for the webroot - it's in the $_SERVER['DOCUMENT_ROOT']
variable.
What you're looking for is not a webroot but rather a URL.
You can get your current URL like so:
$protocol = strpos($_SERVER['SERVER_SIGNATURE'], '443') !== false ? 'https://' : 'http://';
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Here is one way of doing it:
$web_root = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/";
OUTPUT -->http://website.com/parent_folder/