PHP Classes

PHP Cloudflare IP GEO: Get the IP of a user of a site that uses Cloudfare

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 134 All time: 9,306 This week: 39Up
Version License PHP version Categories
cfgeo 1.0.0GNU General Publi...5.6Networking, PHP 5, Geography
Description 

Author

This class can get the IP of a user of a site that uses Cloudfare.

It takes the current user IP address from the HTTP headers that were sent to the server.

If the current site accesses are handled by Cloudflare, it checks specific headers set by Cloudflare servers that pass the current user computer IP address as well the two letter code of the country associated to that IP address.

The class can also return the name of the country associated to the current user country code.

Innovation Award
PHP Programming Innovation award nominee
June 2019
Number 10
Cloudflare is a popular proxy service that can help many sites to handle requests for their pages faster by serving those pages from servers spread around the world that cache the contents of the pages.

When a browser is used to access a site that is handled by Cloudflare, the actual site server does not see the IP address of the computer that runs the browser in the same way that happens when a site is not using Cloudflare to serve its pages.

The actual IP address that the user computer uses is passed to your site by Cloudflare using specific request variables.

This package can get the user IP address passed by Cloudflare, as well the country where the user is located.

Manuel Lemos
Picture of Meraj-Ul Islam
Name: Meraj-Ul Islam <contact>
Classes: 3 packages by
Country: Bangladesh Bangladesh
Innovation award
Innovation award
Nominee: 1x

Example

<?php
// require the class
require_once('class.cfgeo.php');

// initialize the class
$cf = new cfGeo;

// get user's IP
echo 'IP: '.$cf->getClientIp().'<br>';

// get user's county code in ISO format
echo 'Country Code: '.$cf->getCountryCode().'<br>';

// get user's country name
echo 'Country Name: '.$cf->getCountryName().'<br>';

/* Your site need to be intergrated to cloudflare */
?>


Details

There are three functions available in this class. Call them individually along with the class. To get ip address from cloudflare, call: getClientIp() To get country code from cloudflare, call: getCountryCode() To get country from cloudflare, call: getCountryName()

  Files folder image Files (3)  
File Role Description
Plain text file class.cfgeo.php Class The main class
Accessible without login Plain text file example.php Example Example usage of the class
Accessible without login Plain text file readme.txt Doc. The documentation file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:134
This week:0
All time:9,306
This week:39Up