IP

How to determine user geolocation data
How to determine user geolocation data

To determine the user's location, we need their IP address and a database with a list of IP addresses and their associated geographic data. However, since we've already implemented a solution using a database in this article, today we’ll make the task slightly more complex and interact with an external service via its API to retrieve all the information we need — time zone, latitude, longitude, country, and city.

read more...

How to determine user IP in PHP
How to determine user IP in PHP

In this article, we’ll look at one of the common tasks in PHP programming — specifically, how to determine the user's IP address in PHP.

If I’m not working with a framework (such as Yii), I use one of the ready-made solutions in the form of the following function:

function user_ip()
{
	if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown"))
	{
		return getenv("HTTP_CLIENT_IP");
	}
	else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
	{
		return getenv("HTTP_X_FORWARDED_FOR");
	}
	else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
	{
		return getenv("REMOTE_ADDR");
	}
	else if(!empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
	{
		return $_SERVER['REMOTE_ADDR'];
	}
	
	return NULL;
}

read more...

How to determine the region by IP in PHP using the DB
How to determine the region by IP in PHP using the DB

In this article, we’ll look at one way to determine a user’s location based on their IP address — specifically, using an existing database of regions and assigned IP address ranges.

There are many such databases available. I happened to work with a database from ip2location.com. I can’t say anything bad about it — it correctly identified my region and those of some of my clients.

The first thing we need is to download the database file. They provide it in CSV format, and you can get it from this page.

Importing the IP and Region Data File

Follow the instructions in the description to save time. Initially, I tried importing the data via phpMyAdmin using CSV import, and that took significantly longer than a console-based import.

read more...