Skip to content

Visitor and request context

mozrin edited this page Nov 22, 2026 · 4 revisions

Visitor and request context

The released facade exposes two reads. Both use the active request and return immutable snapshots without making network requests.

use Moztopia\Cloudflare\Facades\Cloudflare;

$visitor = Cloudflare::visitor();
$context = Cloudflare::requestContext();

Visitor properties

All properties below are nullable strings:

  • countryCode: a country code validated against the Symfony country-code dataset, from CF-IPCountry.
  • continentCode: one of AF, AN, AS, EU, NA, OC or SA from CF-IPContinent.
  • region: estimated region/state name from CF-Region.
  • regionCode: region code from CF-Region-Code.
  • city: estimated city from CF-IPCity.
  • timezone: a validated PHP timezone identifier from CF-Timezone.
  • countryMarker: Cloudflare's XX unknown marker or T1 Tor marker.

When a country marker is present, countryCode remains null. A marker is not an ordinary country.

Location describes Cloudflare's estimate for the visitor IP. It is not precise physical location, verified residence, language preference, delivery eligibility or authorization.

Unreleased additional visitor properties

Source b52f0eeb485f67119f209094ce546096427358bf implements these additions toward 0.10.0 Cirrus; they are absent from tagged 0.9.1:

  • latitude: nullable float parsed from CF-IPLatitude, bounded from −90 through 90 inclusive.
  • longitude: nullable float parsed from CF-IPLongitude, bounded from −180 through 180 inclusive.
  • metroCode: nullable numeric identifier string from CF-Metro-Code, accepting 1–16 ASCII digits and preserving leading zeroes. The length bound is package validation, not a claimed Cloudflare wire limit.

Coordinates accept bounded signed decimal strings and return finite floats. Exponent notation, non-finite values, duplicate headers, invalid UTF-8/control characters and values outside the coordinate bounds are rejected. Each field is independent: an invalid latitude does not discard a valid longitude. Existing constructor arguments retain their positions; the three nullable properties are appended. Manually constructed snapshots remain application-supplied data.

Zero is valid. Avoid a truthiness check in Blade:

@if ($visitor->latitude !== null && $visitor->longitude !== null)
    <p>Estimated coordinates: {{ $visitor->latitude }}, {{ $visitor->longitude }}</p>
@else
    <p>Location coordinates unavailable</p>
@endif

Rich metadata depends on the zone’s enabled Add visitor location headers Managed Transform and a verified trusted ingress. Country availability alone does not prove the richer fields are supplied. Cloudflare describes latitude, longitude and metro wire fields as strings; the coordinate float results are a deliberate SDK contract. These remain approximate IP-derived metadata, with no reverse geocoding, account secret, locale change or hidden network call.

Postal-code support is excluded from the initial 1.x requirements and has no promised release target. Missing geographic values remain unknown without a GeoLookup dependency.

Request context properties

  • trusted: a boolean indicating whether the configured immediate peer was accepted; false by default.
  • clientIp: a nullable valid IPv4/IPv6 string from CF-Connecting-IP.
  • rayId: a nullable Ray identifier from CF-Ray: 16–32 hexadecimal characters followed by a hyphen and a three-letter data-centre suffix.
  • scheme: nullable http or https from CF-Visitor JSON.

The Ray suffix identifies Cloudflare infrastructure involved in the request; it is not the visitor's region. Worker subrequests and Pseudo IPv4 can affect client-IP semantics, so consult Cloudflare's documentation for your deployment.

Invalid or missing values

Absent, untrusted or malformed values return null. Parsing rejects multiple values where one is expected, invalid UTF-8, control characters and malformed structured fields. Location header values are limited to 256 bytes and must still be escaped when rendered.

An accepted peer may supply only some fields. Always check the particular field needed by the application instead of treating trusted as a complete-data flag.

Snapshots and long-lived workers

The service resolves the current request on every call. A previously returned object remains a snapshot of the request that produced it; it is not updated in place. Resolving the service in a warmed worker does not intentionally retain a previous visitor.

Use the returned snapshot within the current application operation. Do not store a visitor snapshot in a global singleton for reuse across visitors.

Source definitions

Maintained by-dolly.

Clone this wiki locally