tags:

views:

872

answers:

5

Hi,

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?

I am very thankful in advance, Regards

A: 

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.

Jeremy Stanley
If you do choose this method, be aware that it can be affected by the request headers.
Tom Wright
Indeed. Theres url rewriting. But I do not want the path, I just want to check the domain name. Any way around?
Ahmad Fouad
+10  A: 

If you need domain name, use:

$_SERVER['HTTP_HOST']
vartec
Oh, this one worked fine!! even with url rewriting on. Thanks
Ahmad Fouad
+4  A: 

Depending on what you want, I'd use one of the following:

  1. $_SERVER['PHP_SELF'] for the script file location
  2. $_SERVER['SERVER_NAME'] for the host name

From the php docs

EDIT: Maybe PHP_SELF isn't the best. See comments.

Tom Wright
As I know, using $_SERVER['PHP_SELF'] is an absolute risk. http://is.gd/qPcb
Sepehr Lajevardi
+8  A: 

When in doubt

var_dump($_SERVER);
Mike B
+3  A: 
foreach($_SERVER as $key=>$value) //var_dump($_SERVER);
  echo "$_SERVER[".$key."] = ".$value."<br />";

Additionally as far as I know, making use of unsanitized $_SERVER["PHP_SELF"] could be an absolute security risk.

Sepehr Lajevardi