PHP Code for Show user information like IP address, useragent and others

PHP Code for Show user information like IP address, useragent and others

You might have seen some of the sites showing you the information like “Your IP address is 127.0.0.1 ” or “You are using Firefox browser” etc. Its really easy to display or get these user or visitor information using PHP codes. Here in this post, we will write a simple php code for displaying a visitor’s IP address, browser info and where that user came from (Http_Referrer i.e from which url the user clicked to get to your current page).

This will be useful for you to keep track of your visitors or optimize the webpage for your users in a specific way depending on the browser or ip address etc.

<?php
/**
* Show user information like IP address, useragent
**/

$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$referrer = $_SERVER['HTTP_REFERER'];

 if ($referred == "") {
  $referrer = "This page was accessed directly";
  }

echo "<b>Visitor IP address:</b><br/>" . $ip . "<br/>";
echo "<b>Browser (User Agent) Info:</b><br/>" . $browser . "<br/>";
echo "<b>Referrer:</b><br/>" . $referrer . "<br/>";

?>

PHP Code Output

Visitor IP address:
 127.0.0.1

 Browser (User Agent) Info:
 Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

Its really a simple PHP code, but still useful for you to track your website visitors and provide a better web browsing experience for them.


Posted by aeterno

The author and creator Mimata from Indonesia who founded Mimata Back in 2018. He is passionate about all things tech and knows the Machine Learning, IoT, Micro Controller, and many others

Leave a Reply