PHP get url | How to get current URL and IP by $_SERVER

PHP get URL

The PHP $_SERVER, which is a super global, contains useful information that can be used to get complete current URL of the web page.

See the example to get current URL

See the example below, that explains how getURL() function combines different pieces of PHP $_SERVER and displays the current URL.

Get URL example in PHP

The following example can be used to get the current URL in PHP. It creates a function getURL() that simply executes the statements to get the current URL. A variable $whichprotocol is returned by this function that uses PHP $_Server variables: HTTP_HOST and REQUEST_URI.

The later part simply calls this function and displays the current URL.

Experience this example online



PHP get IP address example

The example below shows how to use $_SERVER, super global, to return current IP address by using SERVER_ADDR element.



 

The $_SERVER[‘SERVER_ADDR’]  is used to return server’s IP address.


Was this article helpful?

Related Articles

Leave A Comment?