Forwarded header (RFC 7239) parsing library for PHP. https://packagist.org/packages/madeorsk/forwarded
Go to file
2023-05-26 14:28:47 +02:00
src Add helper functions in Forwarded class. 2023-05-26 14:18:58 +02:00
tests First version of the library, with unit tests. 2023-05-26 02:25:15 +02:00
.gitignore First version of the library, with unit tests. 2023-05-26 02:25:15 +02:00
composer.json Set library version to 1.0.0. 2023-05-26 14:21:00 +02:00
composer.lock Set library version to 1.0.0. 2023-05-26 14:21:00 +02:00
README.md Add README to present the library and show how to use it. 2023-05-26 14:28:47 +02:00

Forwarded

Forwarded is a PHP library which parse the Forwarded request header as defined in RFC 7239. The Forwarded header is a standardized method for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer, for example.

Forwarded is distributed as a composer library.

Getting started

Just add the dependency to your composer project:

composer require madeorsk/forwarded

How to use

The easy way

Usually, you just want to get the Forwarded header from your current request (for example when using PHP-FPM), or even just the IP address of the origin of the request.

<?php

use Madeorsk\Forwarded\Forwarded;

$forwarded = Forwarded::get(); // Getting the parsed Forwarded header.

$originIp = Forwarded::getOriginIp(); // Getting the IP address of the origin request from the parsed Forwarded header.

If you need more control

If you cannot obtain the header content from the classic standard way, you can call directly the Forwarded header parse.

<?php

use Madeorsk\Forwarded\Parser;

// Get a Forwarded parsed header from its raw string.
$forwarded = (new Parser())->parse("for=192.0.2.43,for=\"[2001:db8:cafe::17]\",for=unknown");

// Get the Forwarded header content parsed in a raw associative array from its raw string.
$forwardedAssoc = (new Parser())->parseAssoc("for=192.0.2.43,for=\"[2001:db8:cafe::17]\",for=unknown");