This is odd, but for some reason the $_SERVER["SCRIPT_URI"]
will not return the domain name when I am in child/sub-pages but will only work on the main page. Not sure if its due to the script (WordPress) or host, but please can you suggest any reliable solution to retrieve the domain name with PHP?
11 Answers
If you need domain name, use:
$_SERVER['HTTP_HOST']
-
Oh, this one worked fine!! even with url rewriting on. Thanks Commented Apr 4, 2009 at 21:51
When in doubt
error_log(print_r($_SERVER,true));
Edit note:
Outputting server variables to browser is very bad practise and should be instead output to the PHP error log.
Depending on what you want, I'd use one of the following:
- $_SERVER['PHP_SELF'] for the script file location
- $_SERVER['SERVER_NAME'] for the host name
EDIT: Maybe PHP_SELF isn't the best. See comments.
-
2You should probably use
$_SERVER['SCRIPT_NAME']
instead of PHP_SELF. Commented May 27, 2011 at 14:38
This might be due to URL rewriting, you can try $_SERVER['REQUEST_URI'] instead if you want the path that was called in the url.
-
If you do choose this method, be aware that it can be affected by the request headers. Commented Apr 4, 2009 at 21:50
-
Indeed. Theres url rewriting. But I do not want the path, I just want to check the domain name. Any way around? Commented Apr 4, 2009 at 21:50
I was using $_SERVER[SCRIPT_URI] on my website http://www.a2zidx.com and a number of subdirectories like http://howto.a2zidx.com
I have had no problem with this for years and today got an error on a number of sites where $_SERVER[SCRIPT_URI] was not assigned. After contacting my isp they claim they have not made any changes but $_SERVER[SCRIPT_URI] no longer works. getenv('SCRIPT_URI') does not fail but returns a null string. Why this should happen suddenly after so many years I do not know. I ended up calling a function to go through various options to extract the filename which is what I wanted. Hope this covers everything. I had trouble including the function but checked "SCRIPT_NAME" "PHP_SELF" "SCRIPT_FILENAME "SCRIPT_URI"
Hope his helps.
If anyone knows why SCRIPT_URI would suddenly stop working I would love to know. The server is currently running Apache 2.4.
-
The links look a lot like spam, please consider replacing them for generic ones like example.com– NorriusCommented Feb 27, 2018 at 21:44
On Apache, SCRIPT_URI only exists when mod_rewrite is enabled.
Apache requires:
rewrite_module (note: use platform specific load syntax)
and
RewriteEngine On
statements in the Apache configuration file at the appropriate locations.
Then restart Apache, and it should work fine.
Also, SCRIPT_URI does not exist, the right syntax is REQUEST_URI:
echo $_SERVER["REQUEST_URI"];
It returns the whole path without the host and domain.
-
1
SCRIPT_URI
is not necessarily wrong. It is documented in the Apache docs as an environment variable maintained by mod_rewrite. However, as the OP has found, it's not always set (for whatever reason). But... when it is set, it's not the same asREQUEST_URI
.SCRIPT_URI
contains an absolute URL, whereasREQUEST_URI
is a root-relative URL ("without the host and domain").– MrWhiteCommented Oct 12, 2016 at 22:31
var_dump($_SERVER)
and see what suites your needs.
P.S. Making use of unsanitized $_SERVER["PHP_SELF"]
could be a security risk.
Late late reply. I use this piece of script to store information in my db. Hope this might help someone. How to get your full url and the ip address of the client access from.
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
$url = $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
echo $ip;
echo "<br>";
echo $url;
SCRIPT_URI
is not fond because it has no any value when your site run with root domain. it only come when you working with sub-directory.
HTTP_HOST
is not good idea when we working with sub-directory
use following code to get SCRIPT_URI
:
$SCRIPT_URI = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($_SERVER['SCRIPT_URL']),'/');
-
If HTTP request forwarded by proxy, the REQUEST_SCHEME may return "http" even if original request was made by secure https:// protocol. Check $_SERVER['HTTP_X_FORWARDED_PROTO'] additionally and use it instead of REQUEST_SCHEME if request was forwarded. Commented Apr 7, 2021 at 0:10
If your browser formats JSON
documents nicely and no output has taken place, inserting the following will result in something more readable than var_dump
:
header('Content-Type: application/json');die(json_encode($_SERVER));
phpinfo()
will also provide a list of all $_SERVER
values and so much more!
$_SERVER['REQUEST_URI']
SCRIPT_URI
is documented in the Apache docs as an environment variable maintained by mod_rewrite - so from that it should be present in the$_SERVER
superglobal. However, as the OP has found, it's not always set (for whatever reason).