Rella
Rella

Reputation: 66965

How to determine which URL accessed the script

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

Upvotes: 1

Views: 116

Answers (3)

Bob Fanger
Bob Fanger

Reputation: 29907

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.

Upvotes: 2

Tim
Tim

Reputation: 813

$_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

Upvotes: 0

Michael Mrozek
Michael Mrozek

Reputation: 175585

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

Upvotes: 0

Related Questions