Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current domain + directory in php

Tags:

html

php

So, I've found a way to get the current directory using dirname(__FILE__) and getting the domain with $_SERVER['HTTP_HOST']. While both of these are well and good, they aren't quite what I need them to be.

For instance, if I have a script on http://mydomain.com/scripts/myscript.php, I'd like to get http://mydomain.com/scripts/. I feel like there should be an easy way to do this and that I've somehow overlooked something.

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

like image 426
Jim P Avatar asked Sep 21 '12 14:09

Jim P


1 Answers

Try:

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

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

like image 178
Louis Huppenbauer Avatar answered Sep 30 '22 10:09

Louis Huppenbauer