Getting current domain + directory in php

So, I found a way to get the current directory using dirname(__FILE__) and get the domain using $_SERVER['HTTP_HOST'] . Although both are good and good, they are not exactly what I need.

For example, if I have a script at http://mydomain.com/scripts/myscript.php , I would like to get http://mydomain.com/scripts/ . I feel that there should be an easy way to do this, and that I somehow forgot something.

As an aside, I'm currently using a script in a cloud shared hosting environment, so the directory structure is somewhat odd.

+4
source share
3 answers

Try:

 <?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?> 
+2
source

Try:

 <?php echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); 

The only problem is that dirname returns the parent directory, so if you go directly to http://domain.com/scripts/ , you just get http://domain.com/ with scripts. http://domain.com/scripts/script.php correctly resolves http://domain.com/scripts/ though.

+4
source
 function url_part(){ $http=isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; $part=rtrim($_SERVER['SCRIPT_NAME'],basename($_SERVER['SCRIPT_NAME'])); $domain=$_SERVER['SERVER_NAME']; return "$http"."$domain"."$part"; } echo url_part;//htts://www.example.net/test 
0
source

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


All Articles