2

How would you detect if a user's IP address is visiting the page for the first time in php?

I've tried this code but it uses cookies, and if I remove the cookies in my browser this no longer works, obviously...

if (!isset($_COOKIE['firsttime'])){
    setcookie("firsttime", "no", time()+3600 ); /* 1 hour */
    echo "<script type='text/javascript'>alert('YES');</script>";
    exit();
} else{
    echo "<script type='text/javascript'>alert('NO');</script>";
    exit();
}

How to detect it by IP?

bandrade
  • 143
  • 1
  • 6
  • Did you mean how to get the ip address from client? – brrystrw Jul 07 '14 at 19:44
  • 4
    many people have a dynamic ip allocation so while a cookie can be deleted its probably still more reliable – andrew Jul 07 '14 at 19:45
  • I need to detect the first visit by user client ip, not by cookies.. No problem if this is not more reliable :) Please help –  Jul 07 '14 at 19:51
  • 1
    IP != person, you not detecting anything with this approach –  Jul 07 '14 at 20:07

3 Answers3

1

You can typically get a visitor's IP address by grabbing it from $_SERVER['REMOTE_ADDR'] (as mentioned by @andrew).

There are cases where this may not work as expected because of proxies, etc... (look here) but in general it will give you what you are looking for.

Once you have the IP address, you can persist it to a database, and then look it up every time someone visits, looking for an existing record. No results means it's a new IP.

However, this is not a very good method of determining if a visitor has already been to your website.

The most reliable method (although not perfect), is by using cookies. When a visitor comes to your website, set a cookie... this is still the best way to track/tag a visitor.

You may be interested in experimenting with browser fingerprinting, although it is not very reliable as even minor changes to browser configurations can change the fingerprint. Unless you are prepared to write complex algorithms for detecting minor changes and calculating the "probability" of a current fingerprint to match one in the database, I would recommend sticking with cookies.

Community
  • 1
  • 1
Petar Zivkovic
  • 970
  • 10
  • 20
0

$_SERVER['REMOTE_ADDR']; will give you the users current ip address provided they are not proxied.

But because of isp's handing out dynamic ip address I wouldn't count on this being a reliable way to track the first visit of your users

andrew
  • 9,313
  • 7
  • 30
  • 61
  • where have I to put this in my code? Can you post an example please? –  Jul 07 '14 at 19:53
  • where you put it depends on what you want to do with it. ` – andrew Jul 07 '14 at 19:55
  • What if I want to get the "YES" alert and "NOT" alert like in my example? I've tried but it does not work...Please help I'm new to this :) –  Jul 07 '14 at 19:58
  • well you would need to store the ip address of each of your visitors into a database and the compare them on subsequent visits which would warrant a separate question entirely – andrew Jul 07 '14 at 20:02
  • Unfortunately I have not database :( Is there some other way? –  Jul 07 '14 at 20:05
  • there are other options but they merely emulate a database such as an xml or json file – andrew Jul 07 '14 at 20:07
0

There is no perfect way of doing this as even an IP Address can change. If the user has a Dynamic IP Address, the address can change frequently.

Either way, using the user's IP Address, you will have to store the address somewhere on your server. If you have a database set up, like MySQL, you can store the IP Address in a table.

See the MySQLi documentation page in the PHP manual.

Once you are familiar with MySQL, you can write code similar to this:

$mysqli = new mysqli("example.com", "user", "password", "database");
if (!$mysqli->connect_errno) {
    //You should validate the IP Address
    $ip = $_SERVER['REMOTE_ADDR'];
    $res = $mysqli->query("SELECT IPAddress FROM tbIP WHERE IPAddress='{$ip}' LIMIT 1")
    if($res !== false){
        if($res->num_rows > 0){
            //Found
            echo "<script type='text/javascript'>alert('YES');</script>";
        } else {
            //Not found
            echo "<script type='text/javascript'>alert('NO');</script>";

            //Here you will probably want to INSERT a new record into the Database
            //tracking the current user.
        }
    }
}

Again, I'd recommend reading up on MySQL.

If you don't have access to a database and you have eliminated cookies, you could write the IP Addresses to a file instead (replacing the MySQL calls with calls to read from the file. This is not usually a good choice, however if you have no alternatives, you don't have many options.

noahnu
  • 3,479
  • 2
  • 18
  • 40