Register unique ID from IP address

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts:282
Location:Holland Rotterdam

Register unique ID from IP address

I have a problem it does not register all IP-addresses from the visitors with a Unique Key made from their IP-address.
I see also that some visitors have a different Unique Key made from their IP-address, this I saw in another table what register all IP-addresses without a Unique Key.
To get a Unique Key I use str_replace from the IP-address
but some str_replace gives a different number then their IP-address (this i don`t mind)

Code: Select all

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$today = date("Y-m-d-H.i.s");
$intip = str_replace(".","",$ip);

$details = json_decode(file_get_contents("ipinfo.io/{$ip}"));
echo $details->country; // -> "US"
?>
- This is the SQL INSERT INTO code

Code: Select all

<?php
$con=mysqli_connect("127.0.0.1","visitors","123456","visitors");
// Check connection
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

// Perform queries 
mysqli_query($con," INSERT INTO ip_register (id,ip_adress,country,date) 
 VALUES ('$intip','$ip','$details->country','$today') ON DUPLICATE KEY UPDATE ip_adress ='$ip' ");

mysqli_close($con);
?>
Is their another posibility were i can give visitors a Unique Key (id) from their IP-address, that will register all visits ???

Admin Posts:805
You cannot use unique id from ip address because some visitor can have dinamic ip.
For example, in my case when i connect to internet the ip changes; this is from the internet provider which gives dinamycally ip.
- You can make unique id for registered users, when they log-in, based on their login data (name or email).

JanMolendijk Posts:282
When I use a automatic Key insert (ID) then it registers with $ip = $_SERVER['REMOTE_ADDR']; all IP-addresses from the visitors... But then I get to many the same IP-Registrations in my database... Can I Delete all the same IP-address registrations in the Table & keep only FIRST insert record from the same IP-address ??? (so I get one unique ID)

JanMolendijk Posts:282
I let go of my imagination, and tried with loading of iframes, but then it did not register either...

After that I changed my code into this, I don`t
known if it is completely correct, but it is working...

Code: Select all

<?php
// connect to the "tests" database
$conn = new mysqli('127.0.0.1', 'visitors', '123456', 'visitors');
// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT * FROM `ip_register` WHERE ip_adress='$ip' LIMIT 1"; 

// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
  // output data of each row from $result
  while($row = $result->fetch_assoc()) {

$sql = "UPDATE ip_register SET visit_counter='11', last_visit='$last_visit' where ip_adress='$ip'";

  }
}
else {

mysqli_query($conn," INSERT INTO ip_register (ip_adress,country,date) 
 VALUES ('$ip','$details->country','$today')  ");

  echo '';
}
$conn->close();
?>
I get an ID as Unique Key for every IP-address what only once
register in the table & after updates the visit_counter by eatch visit.

Furter I don`t mind when more people uses the same IP-address

Similar Topics