Insert Ip to database

Codes here !

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Mar 12, 2018 5:02 am

Mon Mar 12, 2018 5:04 am


I am a new user of PHP and MySql. I would like to capture Public IP address of every person who displays my website and I need to insert this IP variable into my database.table.

I've got this code in PHP but it doesn't work properly. Please, can you suggest me what would be the possible ways to do it?

How can I run this file.php from my FTP or Website to check?
I can send that file.php on my FTP as well.

I have created a new database and table and file.php with this code below:

Code: Select all

$servername = "";
$database = "*******";
$username = "*******";
$password = "*******";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection

if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
echo "Connected successfully";

 // Get referrer:
 //   $ref=@$HTTP_REFERER; 
 //   echo "<b>Referrer of this page  = $ref </b><br>";

    // Get IP:
 //   echo "<b>IP Address= $ip</b><br>";

$sql = "INSERT INTO ip_addresses (IP_Address) VALUES ($ip)";
// $sql = "INSERT INTO ip_addresses (IP_Address) VALUES ('get_the_user_ip')";
if (mysqli_query($conn, $sql)) {
      echo "New record created successfully";
} else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);


User avatar
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Mon Mar 12, 2018 6:33 am

Here are three resources I would consult that I found after doing Google searches...

How to obtain IP addresses:

Build a visitor tracking system (but you might need to update database access/write code to use PDO): ... -with-php/

Inserting data into your database using php (generalized):

php-forum Fan User
php-forum Fan User
Posts: 601
Joined: Fri Apr 01, 2016 2:18 am

Mon Mar 12, 2018 7:14 am

but it doesn't work properly
what should this mean to anyone not familar with your mission?

Post Reply