oihana/php-http

Composable PHP HTTP toolkit โ€” client IP detection (X-Forwarded-For chain walking + CIDR + IPv4/IPv6), typed Set-Cookie header builders, route pattern utilities (Slim optional segments + Casbin patterns), user-agent parsing. PSR-7 compatible, zero magic strings.

Maintainers

Package info

github.com/BcommeBois/oihana-php-http

pkg:composer/oihana/php-http

Statistics

Installs: 5

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-05-26 17:34 UTC

This package is auto-updated.

Last update: 2026-05-26 17:34:57 UTC


README

Oihana PHP Http

Composable PHP HTTP toolkit. Part of the Oihana PHP ecosystem, this package provides battle-tested helpers for HTTP-facing code: client IP detection against reverse proxies, typed Set-Cookie header builders, route pattern utilities and user-agent parsing โ€” all PSR-7 compatible, zero magic strings.

Latest Version Total Downloads License

๐Ÿ“š Documentation

Full API reference (generated with phpDocumentor): https://bcommebois.github.io/oihana-php-http

User guides (FR + EN) live under wiki/.

๐Ÿ“ฆ Installation

Requires PHP 8.4+. Install via Composer:

composer require oihana/php-http

โœจ What you can do

  • Detect the real client IP behind a chain of trusted reverse proxies. Walks X-Forwarded-For right-to-left, skips trusted hops via CIDR matching, falls back on Forwarded (RFC 7239), X-Real-IP and REMOTE_ADDR. Validates IPv4 and IPv6, normalizes IPv4-mapped IPv6 (::ffff:1.2.3.4 โ†’ 1.2.3.4), strips IPv6 zone IDs.
  • Anonymise IPs for GDPR-friendly logging โ€” truncateIpToSlash24() (IPv4 โ†’ /24, last octet zeroed) and truncateIpToSlash48() (IPv6 โ†’ /48, last 80 bits zeroed), or anonymizeIp() for a single entry point that routes to the right helper based on the address family.
  • Build Set-Cookie headers with typed attributes: HttpOnly, Secure, SameSite=Strict|Lax|None, Domain, Path, Max-Age.
  • Expand Slim route patterns carrying optional bracket segments (/users[/{id:[0-9]+}]) into their cartesian product of concrete routes โ€” useful for permission seeding and route-by-route authorization.
  • Translate Slim route patterns into Casbin patterns by collapsing {placeholder} segments into *.
  • Read the request User-Agent string via getUserAgent().

Under the hood

  • A consistent set of typed enums and constants โ€” CookieAttribute, CookieOption, SameSite.
  • Pure PSR-7 โ€” no framework lock-in. Works with Slim, Laravel, Symfony HTTP Foundation (via PSR-7 bridge), Hyperf, RoadRunner, etc.
  • Strongly-typed enums and constants โ€” no magic strings anywhere.

โœ… Running tests

Run all tests:

composer test

Run a specific test file:

composer test ./tests/oihana/http/helpers/ips/GetClientIpTest.php

๐Ÿ› ๏ธ Generate the documentation

We use phpDocumentor to generate documentation into the ./docs folder.

composer doc

๐Ÿงพ License

Licensed under the Mozilla Public License 2.0 (MPLโ€‘2.0).

๐Ÿ‘ค About the author

  • Author: Marc ALCARAZ (aka eKameleon)
  • Email: marc@ooop.fr
  • Website: https://www.ooop.fr

๐Ÿ”— Related packages

  • oihana/php-core โ€“ core helpers and utilities: https://github.com/BcommeBois/oihana-php-core
  • oihana/php-enums โ€“ typed constants & enums: https://github.com/BcommeBois/oihana-php-enums
  • oihana/php-files โ€“ file system helpers: https://github.com/BcommeBois/oihana-php-files
  • oihana/php-reflect โ€“ reflection and hydration utilities: https://github.com/BcommeBois/oihana-php-reflect
  • oihana/php-auth โ€“ Casbin RBAC + JWT/OIDC authorization toolkit: https://github.com/BcommeBois/oihana-php-auth