Skip to content

Detect mobile and tablet devices from the request - #1118

Open
jarovarga wants to merge 1 commit into
cakephp:5.xfrom
jarovarga:ticket-1114-mobile-detectors
Open

jarovarga wants to merge 1 commit into
cakephp:5.xfrom
jarovarga:ticket-1114-mobile-detectors

Conversation

@jarovarga

Copy link
Copy Markdown

Fixes #1114.

  • Create MobileDetect with autoInitOfHttpHeaders disabled and pass it the headers from the request's server params instead of $_SERVER.
  • Only pass the headers MobileDetect knows (User-Agent, mobile and CloudFront headers), mirroring its own autoInitKnownHttpHeaders(). Passing all server params would include non-string values such as argv, which trigger "Array to string conversion".
  • Flatten array values set via withHeader(), which MobileDetect cannot handle.
  • Set an empty User-Agent when none is present, because MobileDetect throws without one.
  • Add RequestDetectorTest covering constructed requests, changed headers, array values and requests without a User-Agent.

The detectors created MobileDetect without arguments, so it read the
headers from the global $_SERVER instead of the request they were
called on. Constructed requests in tests, sub-requests and headers
changed by middleware were therefore ignored.

Pass the request's known User-Agent, mobile and CloudFront headers to
MobileDetect instead, flattening array values set via withHeader(),
and fall back to an empty User-Agent so that MobileDetect does not
throw.

Fixes cakephp#1114
@jarovarga
jarovarga force-pushed the ticket-1114-mobile-detectors branch from 92b1b67 to ef788c7 Compare October 13, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mobile and tablet detectors ignore the request they are called on

1 participant