Auto Recognise Users via IP Address Through PHP/MySQLi v2 PHP Script

Auto Recognise Users via IP Address Through PHP/MySQLi v2 PHP Script

This tutorial will show you how to quickly identify your website users through their IP.

First we will need a database (mine is ‘fln’), a table – ‘test’, and columns with data. My columns are;

id, int, 5 length, primary key, auto increment
username, varchar, 255 length
ip, varchar, 255 length

The IP field does not have to be 255 characters long but it will help to avoid errors in case we accidentally input some data other than an IP address.

Now we need to connect to our database through PHP and MySQLi, we do this through a connection statement.


          $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name


Next we want to get the current visitor’s IP address. We can do this through a simple Server variable ‘Remote Address’. We store it in a string variable named ‘ipaddr’, standing for Internet Protocol Address.



    $ipaddr = $_SERVER['REMOTE_ADDR'];


Finally we want to check to see if there are any pre-existing rows containing our visitors IP, if so, we output their possible username. Otherwise we say that there is no username found in association with their IP address.



    $q = mysqli_query($con, "SELECT * FROM `test` WHERE `ip`='$ipaddr'");
    if (mysqli_num_rows($q) > 0) {
          echo 'Is your username ' . mysqli_fetch_array($q)['username'] . '?';
          echo 'No username found by that IP address!';


IP addresses can be easily spoofed, so it is important not to use this as your only verification feature!

You may also want to tell the users that their IP is being logged, for your own security reasons.

All done! Next tutorial (I will link here when it’s online) I will create the second part to this tutorial where we will allow any unrecognised users to register.

You may download my test DB from the button below this post. Please note; the IP is ‘’ for anyone’s localhost, this is because I used my localhost to test this – plus I wouldn’t give out my IP.




Read Me


Find More Auto Recognise Users via IP Address Through PHP/MySQLi v2 PHP Script

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *