Simple php script that track client IP address

i m trying to create a module to create last_seen login history in my website so that i can checkout which client was last login.time & date or its ip address as will.if anybody know about it can suggest any idea any help is appreciated.

Hi @navjot789!

You can create a table in your database logins_log and then add a few lines of code to insert the user id and the IP from which he has logged in.

A sample query command might be this:

mysqli_query($connection, "INSERT INTO logins_log(user_id, ip, date) VALUES('user_id', '" . $_SERVER["REMOTE_ADDR"] . "', '" .  time() ."');");
1 Like

oh sir thankyou let m try!

will it worked on localhost oflline bcz i dont think so?

Yes it will.

It will return 127.0.0.1

Then to read the log implement something like this:


$ret = mysqli_query($connection, "SELECT * FROM logins_log;");
$ret = mysqli_fetch_all($ret); // returns a multidimensional array
//print_r($ret);               // uncoment this for more info

print("<table border=\"1px\">");
print("<tr>");
print("<td class=\"yellow\">User ID</td>");
print("<td class=\"yellow\">IP</td>");
print("<td class=\"yellow\">Date/Time</td>");
print("</tr>");

foreach($ret as $x => $y)  // walk through array
{
    print("<tr>");

    foreach($y as &k => $v) // walk through array keys
    {
        if(preg_match("/[0-9]*/", $v) === 1)
        {
           print("<td>" . date("F j, Y, g:i a", $v) . "</td>");
        }
        else
        {
           print("<td>$v</td>");
        }
    }

    print("</tr>");
}

print("</table>");

I hope it works. I have coded it without testing it…

1 Like

sir g you r the greatest person that i ever met serouslly!:blush:

what was the type of ip column ? varchar or int or?

I was about to suggest VARCHAR…

It’s an IP. It contains more dots '.'

3 Likes

its return only ::1 thats it.remember i m on localhost and having active internet connection

The address of localhost is 127.0.0.1. It should return that one. At least on Windows it returns that.

However it also depends what network settings you have… What OS do you have?

1 Like

currently os i m using is win8 os

On my machine it returns IPv4 style: 127.0.0.1.

On yours it returns IPv6 style: ::1

Doesn’t matter. The script works as expected :slight_smile:

1 Like

thankyou life-saver.:blush:

You’re welcome :blush:

2 Likes