Вопрос

I'm trying to build a database with records of unique visitors to a particular page on my website by obtaining their IP.

The code below fetches the visitor's IP:

$getip = $_SERVER['REMOTE_ADDR'];
echo "<b>IP Address: $getip</b>";

But I realize that this is very unreliable and inaccurate if various internet users are on a specific internet network and they all share the same IP address using different computers (Like a public cyber cafe or see example below).

Example:

XYZ Ltd uses a specific internet connection, if all the employees of XYZ Ltd are connected to the internet via the company internet connection and XYZ Ltd's IP Address is 69.18.107.24. If some of the employee of XYZ visits my page, the code above would not give me unique IP address of the user's computer rather I'll keep getting the company IP address (69.18.107.24) which is inaccurate and deceptive cos the application would see it as the same visitor whereas there are different visitors using the internet connection IP.

How can I fetch unique user IP based on user's computer regardless of whether they are sharing the same internet connection.

I dont want to use PHP cookies cos some browsers do not support it while some users disable it or somthing.

Would be very grateful getting help with this...Thanks!

Это было полезно?

Решение

It is not possible to get the local IPs of computers behind a shared connection. Even if you could get them, they would not be unique, because they are reused - that's the whole point of NAT. You would have a lot of people using 192.168.1.1 or similar addresses.

If you want to differentiate between multiple machines that share the same IP address, you can add another factor, like the user agent string that tends to differ.

For example:

<?php
md5(
    $_SERVER['REMOTE_ADDR'] .
    $_SERVER['HTTP_USER_AGENT']
);
// 80b567b7c7ccfbda75a9712f16ca4429

would give you a hash that would only be identical if two users share the same IP address and have the same browser and operating system version.

Visit Panopticlick to find out more about "browser fingerprinting". Surprisingly, browsers are almost unique in the combination of their configuration. If you bring in client-side analysis with Javascript, where you can access display resolution, installed plugin versions and other details, you can easily differentiate between users that have a shared IP.

Другие советы

You cannot. IP addresses are a data packet delivery mechanism. Nothing guarantees that an IP address in a specific network is unique to one specific machine, there simply is no such correlation. The machine has a unique address inside its network, but this address may not be globally unique (and you cannot get this IP anyway). The public IP of the shared proxy is unique on the public internet, but nothing dictates that it must correspond to one physical machine.

IPs != machines, live with it.

Here is the answer to your question:

<?php

echo "IP address".$_SERVER["REMOTE_ADDR"]

echo "LAN Address".$_SERVER["HTTP_X_FORWARDED_FOR"] 
?>

This is not a sure way to get a unique visitor. The LAN address can be spoofed easily. Also many companies use a DHCP server that with give out new LAN IP's every day. Cookies would be the "only" real way of checking.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top