PHP Classes

File: _docs/IPHeader.md

Recommend this page to a friend!
  Packages of Caleb   PHP Common Class Library   _docs/IPHeader.md   Download  
File: _docs/IPHeader.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Common Class Library
Set of classes that provides common functionality
Author: By
Last change:
Date: 9 months ago
Size: 2,584 bytes
 

Contents

Class file image Download

Documentation for the "IPHeader" class.

Attempts to resolve an originating IP address from a preferred source, or REMOTE_ADDR if the preferred source isn't available.

How to use:

Instantiate the class, supplying as a parameter to the constructor the name of the $_SERVER variable (i.e., the name of the header) that you want the class to prefer as the source for the request's originating IP address.

Example:

<?php
$Obj = new \Maikuolan\Common\IPHeader('X-Forwarded-For');

Some suggested sources to use: | Normally used for: ---|--- HTTP_INCAP_CLIENT_IP | Incapsula reverse proxy. HTTP_CF_CONNECTING_IP | Cloudflare reverse proxy. CF-Connecting-IP | Cloudflare reverse proxy (alternative; if the above doesn't work). HTTP_X_FORWARDED_FOR | Cloudbric reverse proxy. X-Forwarded-For | Squid reverse proxy. Forwarded | Forwarded - HTTP \| MDN. REMOTE_ADDR | Used in about ~99% of cases normally (and the class also falls back to this source anyway if the preferred source specified isn't available).

After instantiating the class, the object will provide three public properties.

public $Resolution = '';

The Resolution property is a string describing the IP address resolved by the instance. It'll contain a valid IPv4 or IPv6 address, or if the instance failed to resolve an IP address, an empty string.

You can use the Resolution property to access the resolved IP address.

Example:

<?php
$_SERVER['Foobar'] = 'for=192.0.2.60;proto=http;by=203.0.113.43';
$Obj = new \Maikuolan\Common\IPHeader('Foobar');
echo $Obj->Resolution;
// Uses "Foobar" to produce: 192.0.2.60

$_SERVER['Foobar'] = 'Not a valid IP address';
$_SERVER['REMOTE_ADDR'] = 'proto=http;by=203.0.113.43;for=192.0.2.61,for=198.51.100.17;';
$Obj = new \Maikuolan\Common\IPHeader('Foobar');
echo $Obj->Resolution;
// Falls back to "REMOTE_ADDR" to produce: 192.0.2.61

public $Source = '';

The Source property is a string describing where the resolved IP address was ultimately sourced from (generally, either the preferred source specified, or REMOTE_ADDR). If the instance failed to resolve an IP address, it'll be an empty string.

public $Type = 0;

The Type property is an integer describing the type of IP address resolved: 4 for IPv4, 6 for IPv6, or 0 otherwise (e.g., if the instance failed to resolve an IP address).

Last Updated: 1 July 2025 (2025.07.01).