User Info with PHP

The following snippets illustrate some of the stuff PHP knows about your website visitors, e.g. the IP address or which page that let them to your page (referer). These are just some of the predefined constants/reserved variables PHP provide, see Predefined Variables.

User's IP address

<?php echo $_SERVER['REMOTE_ADDR']; ?>

34.204.187.106

Info about user's browser

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>

CCBot/2.0 (https://commoncrawl.org/faq/)

Which page did refer the user here

<?php echo $_SERVER['HTTP_REFERER']; ?>

Path to this page (with http query)

<?php echo $_SERVER['REQUEST_URI']; ?>

/docs/scripts/userinfo.php

Path to this page

<?php echo $_SERVER['PHP_SELF']; ?>

/docs/scripts/userinfo.php

Full server path to the website's root

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

/customers/f/4/a/dunweber.com/httpd.www

To see all info PHP knows create and upload a file with the following content and view it in your browser (remember to delete it again after).

phpinfo.php

<?php phpinfo(); ?>

Geo Location

An user's IP tells in which country and region the user is located (approximately and not accounting for VPN connections or proxy servers). This information can be used to display the website in a specific language, use local currency, or provide other locally relevant info. One of many geo location providers is ipapi.co and as example using your IP (34.204.187.106):

ipapi.co request

<?php echo file_get_contents('url as below'); ?>

https://ipapi.co/34.204.187.106/country_name



https://ipapi.co/34.204.187.106/city



https://ipapi.co/34.204.187.106/currency