tags:

views:

59

answers:

3

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

A: 

$_SERVER['HTTP_HOST'] will hold the part of the request URI you're looking for

Michael Mrozek
This should only return the host domain like `www.blah.com', and not the dir or page info (eg: /myfile.php).
Tim
@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
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
+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