Get webroot in PHP

I am using Apache server for PHP . How can I get my root site in PHP, for example http://localhost/testthesis/ ?

+4
source share
4 answers

Relative paths

For the web server root directory, use:

 $folder = '/'; 

For the directory of the extracted script use:

 $folder = './'; 

Absolute paths (from the point of view of the client)

For the web server root directory, use:

 $protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://'; $folder = $protocol . $_SERVER['HTTP_HOST']; 

For the directory of the extracted script use:

 $protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://'; $folder = $protocol . $_SERVER['HTTP_HOST'] . '/' . basename($_SERVER['REQUEST_URI']); 
+14
source

Here is one way to do this:

 $web_root = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/"; OUTPUT -->http://website.com/parent_folder/ 
+8
source

Network root is always only. You will never need a host name or part of a protocol, and root can only be the root server, not any folder or file.

If you need some way, for example /testthesis/ - there are ways, but it has nothing to do with web root .

If you need a file system directory for webroot - it is in the variable $_SERVER['DOCUMENT_ROOT'] .

+5
source

What you are looking for is not a website, but a URL.

You can get your current URL as follows:

 $protocol = strpos($_SERVER['SERVER_SIGNATURE'], '443') !== false ? 'https://' : 'http://'; $url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
+1
source

Source: https://habr.com/ru/post/1303784/


All Articles