How to use ip2country class on CodeIgnitor

Hi Guys,

I recently came up with the requirement of extracting user's IP address and display website's content accordingly for www.dezshira.com . After some explore I found Free ip2country (ip to country) PHP class provides IP database and PHP code to use on your project. But my problem was to make it's successful use on my current MVC platform - codeIgnitor.

Below are couple of quit steps I took to use this fantastic database on my codeIgnitor website , hope this helps somebody :

Step 1:

Download the csv file and import it in a mysql table , in my case I have used the tablename as ip2c.
Importing is quit straight forward , let me know if you don't know how to do this.

Step 2:

Create your helper class and save it on your helper directory :

code ( filename : ip2c_helper.php):


function get_browser_country()
{
$v='';
$v= (!empty($_SERVER['REMOTE_ADDR']))?$_SERVER['REMOTE_ADDR'] :((!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR']: @getenv('REMOTE_ADDR'));
if(isset($_SERVER['HTTP_CLIENT_IP']))
$v=$_SERVER['HTTP_CLIENT_IP'];
$ip = htmlspecialchars($v,ENT_QUOTES);

$ip_num=sprintf("%u",ip2long($ip));

$sq="SELECT country_code,country_name FROM ip2c WHERE ". $ip_num." BETWEEN begin_ip_num AND end_ip_num";
$r=@mysql_query($sq);

if(!$r)
return '';

$row=@mysql_fetch_assoc($r);
$country_name=$row['country_name'];
$country_code=$row['country_code'];
return $row['country_code'];
}

Step 3:

Now you are ready to use it on your respective controller.


$this->load->helper("ip2c");
$this->site->user_country=get_browser_country();

Autoload the helper , if you are using these functions more frequently.

Nope

Nanh... Not a promotion ... that's the example about the problem and it's solution I used on it.

For sure these techniques are for programmer dude ... if someone come up with real questions about what they do not understand ... I am open for help and explanation.

Regards
Kaushal



faint

usually, i just feel faint when i saw lots of codes.
you put on DSA website, is that promotion for company? someone perhaps happy for that...hehe...
bogging is a popular marketing method right now i think.



Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Sorry For The Trouble Guys , this question is for testing whether you are a human visitor and to prevent automated spam submissions.