How do I tell if my script was accessed as http://localhost/myfile.php
or, for example, http://222.22.22.22/myfile.php
? I need to get the 222.22.22.22
part as a string
views:
59answers:
3
A:
$_SERVER['HTTP_HOST']
will hold the part of the request URI you're looking for
Michael Mrozek
2010-05-11 00:04:34
This should only return the host domain like `www.blah.com', and not the dir or page info (eg: /myfile.php).
Tim
2010-05-11 00:24:48
@Tim That's the part he wants. Maybe I made it less clear when I cleaned up his post, sorry for the confusion; I edited it again to make it explicit. In the first revision he does specifically say '222.22.22.22'
Michael Mrozek
2010-05-11 14:29:49
A:
$_SERVER["SCRIPT_URI"]
More info on all of the available $_SERVER variables are in the PHP reference at http://www.php.net/manual/en/reserved.variables.server.php
Tim
2010-05-11 00:19:33
+2
A:
For info about "http://example.com:8080/myfile.php" you've got the following options:
echo $_SERVER['HTTP_HOST'];
"example.com:8080"
echo $_SERVER['SERVER_NAME'];
"example.com"
echo $_SERVER['SERVER_PORT'];
8080
If the server run on port 80 the $_SERVER['HTTP_HOST'] won't contain the port number.
Bob Fanger
2010-05-11 18:57:20