PHP Classes

File: ipclass.php

Recommend this page to a friend!
  Classes of kz   IP 2 Country IP2C   ipclass.php   Download  
File: ipclass.php
Role: Class source
Content type: text/plain
Description: class file
Class: IP 2 Country IP2C
Get country of a given IP address from CSV files
Author: By
Last change: script bug ;)
Date: 13 years ago
Size: 2,086 bytes
 

Contents

Class file image Download
<?
/*

IP 2 Country class
2011.01
Kacper Zbonikowski - POLAND
mobile +48 501079176 email: zbonikowski@o2.pl
Free to non-comercial use


if you use ip_database.csv please add http://webnet77.com/ link on your site


*/
   
class get_user_info{
        private
$ip;
        private
$ex_ip;
        private
$error;
        private
$cip;
        private
$ips;
        private
$user_info;
        function
get_user_info(){
           
$this->get_user_ip();
        }
        function
user_info(){
           
$this->get_user_ip();
           
$this->dist_ip();
           
$this->count_ip();
            if(!
$this->error){
               
$this->inc_db_ips();
               
$this->search_country();
            }
        }
        function
count_ip(){
            if(!
$this->error)
               
$this->cip=$this->ex_ip[3]+($this->ex_ip[2]*256)+($this->ex_ip[1]*256*256)+($this->ex_ip[0]*256*256*256);
        }
        function
search_country(){
            if(!
$this->error)
                for(
$i=0;$i<count($this->ips);$i++){
                    if((
$this->ips[$i][0]>$this->cip)&&($this->ips[$i-1][0]<$this->cip)){
                       
$this->user_info["user_ip"]=$this->ip;
                       
$this->user_info["user_cip"]=$this->cip;
                       
$this->user_info["full_country"]=str_replace("\n","",$this->ips[$i-1][6]);
                       
$this->user_info["3letters_country"]=$this->ips[$i-1][5];
                       
$this->user_info["2letters_country"]=$this->ips[$i-1][4];
                    }
                }
        }
        function
inc_db_ips(){
           
$f=file("ip_database.csv");
            foreach(
$f as $v){
               
$ex=explode(",",$v);
               
$this->ips[]=$ex;
            }
        }
        function
dist_ip(){
            if(
$this->ip!=''){
               
$this->ex_ip=explode(".",$this->ip);
                if(
count($this->ex_ip)<4)$this->error[]='bad IP dist_ip';
            }else{
               
$this->error[]='no IP - dist_ip';
            }
        }
        function
get_user_ip(){
           
$this->ip=$_SERVER[REMOTE_ADDR];
        }
        function
set_ip($ip){
            if(
$ip!=''){
               
$this->ip=$ip;
            }else{
               
$this->error[]='no IP - set_ip';
            }
        }
        function
show_all_info(){
            if(
$this->error){
                echo
$this->ip;
                echo
"Bad IP";
                return;
            }
            echo
$this->cip;
            echo
" ";
            echo
$this->ip;
            echo
"<pre>";
           
print_r($this->user_info);
            echo
"</pre>";
        }
        function
_debug(){
           
$this->show_all_info();
            echo
"<pre>";
           
print_r($this->error);
            echo
"</pre>";
        }
    }
?>