Hi guys, welcome back to mrbarnk.com, today i am here to show you on how to get users ip address with php code.

Now, before you judge me wrong of giving you the wrong code.
First read this well.

  • You don't use this on a local server.: This code might return ::1 on the local engine.

but, don't worry, you can get the right data when you've hosted the code on an online server.

Now, for easier assessment. We need to encode that codes in a function so we'll be able to get the code from every dimension.

function getUserIpAddr(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

echo 'User Real IP - '.getUserIpAddr();

Now, you should be able to get the users ip address through this function.

How To Insert The IP address into the database.

  1. Set the database connection like i explained here.
  2. Get the database connection variable and follow the steps below.
#creating table for the user that will contain the ip addresses
create table `users`(
    id int(10) not null primary key auto_increment,
    `ip_address` varchar(191) not null
);

Now, you can easily create a user table on to the database.

NOTE: but listen, if you are using the previous table we created at the signup system tutorial we created.
You can use the code below to truncate and re-update the table to add the user ip_address.

Use the code below.

ALTER TABLE `users` ADD `ip_address` VARCHAR(191) NOT NULL AFTER `pass`;

Boom! There you go.

Now, start writing your php code.

$ip = getUserIpAddr();
$query = "INSERT INTO `users`(`email`, `pass`, `ip_address`) VALUES ('$emailVariable','$passVariable','$ip')";

echo mysqli_query($conn,$query);

If it returns 0, it's inserted but if it returns 1. It is not inserted.

Update Users Detail With The Ip Adress

$ip = getUserIpAddr();
$query = "UPDATE `users` set `ip_address`='$ip' WHERE `email`='$sessionUserEmail'";

echo mysqli_query($conn,$query);

Boom! The same thing happens here.

1 or 0 determines the status of the code.

Then, you are good to go.

If you love this, make sure you comment and register if you haven't.

Hi guys, welcome back to mrbarnk.com, today i am here to show you on how to get users ip address with php code. Now, before you judge me wrong of giving you the wrong code. First read this well. - You don't use this on a local server.: This code might return ```::1``` on the local engine. but, don't worry, you can get the right data when you've hosted the code on an online server. Now, for easier assessment. We need to encode that `codes` in a function so we'll be able to get the code from every dimension. ```` function getUserIpAddr(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } echo 'User Real IP - '.getUserIpAddr(); ```` Now, you should be able to get the users ip address through this function. ###How To Insert The IP address into the database. 1. Set the database connection like i explained [here](https://mrbarnk.com/t/26/how-to-connect-to-the-database-with-mysqli). 2. Get the database connection variable and follow the steps below. ``` #creating table for the user that will contain the ip addresses create table `users`( id int(10) not null primary key auto_increment, `ip_address` varchar(191) not null ); ``` Now, you can easily create a user table on to the database. NOTE: but listen, if you are using the previous table we created at the [signup system tutorial](https://mrbarnk.com/t/27/how-to-create-a-signup-system-in-php-part-1) we created. You can use the code below to ``truncate`` and re-update the table to add the user ``ip_address``. Use the code below. ``` ALTER TABLE `users` ADD `ip_address` VARCHAR(191) NOT NULL AFTER `pass`; ``` Boom! There you go. Now, start writing your php code. ``` $ip = getUserIpAddr(); $query = "INSERT INTO `users`(`email`, `pass`, `ip_address`) VALUES ('$emailVariable','$passVariable','$ip')"; echo mysqli_query($conn,$query); ``` If it returns 0, it's inserted but if it returns 1. It is not inserted. ###Update Users Detail With The Ip Adress ``` $ip = getUserIpAddr(); $query = "UPDATE `users` set `ip_address`='$ip' WHERE `email`='$sessionUserEmail'"; echo mysqli_query($conn,$query); ``` Boom! The same thing happens here. 1 or 0 determines the status of the code. Then, you are good to go. If you love this, make sure you comment and [register](https://mrbarnk.com/register) if you haven't.

#php, #js, #laravel, #jquery backend developer.

edited Dec 19 '18 at 8:07 pm
Share      

97
0
1
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft