Bhushan Gadekar
Bhushan Gadekar

Reputation: 13805

How to store ip addresses of visitors in database in php?

I have tried this php method to get ip address but it is providing wrong output. the regular method

 <?php     echo $_SERVER['REMOTE_ADDR'];   ?>

does not provide the appropriate output. when i use this method , i get output like

IP address : '::1'

is there any problem in my script? also give the details about storing those ip addresses in database Thanks in advance

Upvotes: 2

Views: 16089

Answers (4)

Joe Kdw
Joe Kdw

Reputation: 2261

The complete one :D :p (unfortunately, this script is in mysql not mysqli/pdo. you can modify this later.

File1.php

<?php
//connect to database
        class usersOnline {

            var $timeout = 300;
            var $count = 0;
            var $error;
            var $i = 0;

            function usersOnline () {
                $this->timestamp = time();
                $this->ip = $this->ipCheck();
                $this->new_user();
                $this->delete_user();
                $this->count_users();
                }

            function ipCheck() {
                if (getenv('HTTP_CLIENT_IP')) {
                    $ip = getenv('HTTP_CLIENT_IP');
                    }
                elseif (getenv('HTTP_X_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_X_FORWARDED_FOR');
                    }
                elseif (getenv('HTTP_X_FORWARDED')) {
                    $ip = getenv('HTTP_X_FORWARDED');
                    }
                elseif (getenv('HTTP_FORWARDED_FOR')) {
                    $ip = getenv('HTTP_FORWARDED_FOR');
                    }
                elseif (getenv('HTTP_FORWARDED')) {
                    $ip = getenv('HTTP_FORWARDED');
                    }
                else {
                    $ip = $_SERVER['REMOTE_ADDR'];
                    }
                return $ip;
                }
            function new_user() {
                $cekIP = "SELECT ip FROM useronline WHERE ip='$this->ip'";
                $resultCekIp=mysql_query($cekIP);
                $countCekIp=mysql_num_rows($resultCekIp);
                if($countCekIp>0){
                    $insert1 = mysql_query ("UPDATE useronline SET timestamp='$this->timestamp', date=NOW(), ip='', distinct_ip='$this->ip'");
                    if (!$insert1) {
                        $this->error[$this->i] = "Unable to record new visitor\r\n";            
                        $this->i ++;
                        }
                    }
                else{
                    $insert2 = mysql_query ("INSERT INTO useronline (timestamp, date, ip, distinct_ip) VALUES ('$this->timestamp',NOW(), '$this->ip', '$this->ip')");
                    if (!$insert2) {
                        $this->error[$this->i] = "Unable to record new visitor\r\n";            
                        $this->i ++;
                        }
                    }
                }
            function delete_user() {
                $delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
                if (!$delete) {
                    $this->error[$this->i] = "Unable to delete visitors";
                    $this->i ++;
                    }
                }
            function count_users() {
                if (count($this->error) == 0) {
                    $count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
                    return $count;
                    }
                }
            }
        ?>

File2.php

<?php
include('file1.php');
$jml_ol = new usersOnline();
if (count($jml_ol->error) == 0) {
    if ($jml_ol->count_users() == 1) {
        echo $jml_ol->count_users() . "<br />";
        echo "Your IP: " . $jml_ol->ipCheck();
        }
    else{
        echo $jml_ol->count_users() . "<br />";
        echo "Your IP: " . $jml_ol->ipCheck();
        }
}
?> 

in body or in div:

<body>
<?php include('File2.php'); ?>
</body>

Upvotes: 4

Ashish Patel
Ashish Patel

Reputation: 76

'::1' means it is your localhost IP Address.

::1 == 127.0.0.1

You can try this one also.

$ip=$_SERVER['REMOTE_ADDR'];
echo "IP address= $ip";

If your application hosted on same machine from where you are trying to request it will always return '::1', It means LocalHost. else it will return client IP Address.

Upvotes: 6

Ryan
Ryan

Reputation: 14659

You can use ip2long which returns an int. Check the return value for -1 or FALSE to make sure a valid IP was returned.

The output you're seeing is the local host in IPv6.

Upvotes: 1

Ancient
Ancient

Reputation: 3057

Here's some sample code

 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

Picked Up from here

Upvotes: 2

Related Questions