Insert Ip to database

Codes here !

Moderators: egami, macek, gesf

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

Mon Mar 12, 2018 5:04 am

Hi,

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

<?php
$servername = "192.168.10.2";
$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:
    $ip=@$REMOTE_ADDR; 
 //   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);
}
mysqli_close($conn);

?>

User avatar
phpRob
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: http://www.plus2net.com/php_tutorial/php_ip.php

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

Inserting data into your database using php (generalized):
https://www.w3schools.com/php/php_mysql_insert.asp

chorn
php-forum Fan User
php-forum Fan User
Posts: 536
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