Skip to content

Commit cf976bf

Browse files
authored
Detect the tracked page URL from REQUEST_URI instead of PATH_INFO (#154)
* Detect the tracked page URL from REQUEST_URI, not PATH_INFO (#141) getCurrentScriptName() builds the auto-detected page URL (the path between host and query string). It preferred $_SERVER['PATH_INFO'], which only holds the trailing path-info segment — so with front- controller / path-info routing (e.g. /dir1/page handled by dir1/index.php) the tracker recorded a truncated '/page' instead of '/dir1/page'. REQUEST_URI already contains the full requested path (PATH_INFO is always just a suffix of it), so use it as the source and drop PATH_INFO entirely; SCRIPT_NAME stays as the fallback when REQUEST_URI is absent. This also aligns the primary source with Matomo core's Url helper. Reported in #141. * Drop the ticket number from an inline test comment Keep issue references in the CHANGELOG and git history, not in code.
1 parent b3e57c4 commit cf976bf

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Attention: this is a major release with breaking changes.
3838
- Event and content tracking requests now send `&ca=1` (custom action), so Matomo no longer falls back to recording them as page views if the handling plugin is disabled (#80).
3939
- The `cip` (override IP) tracking parameter is now URL-encoded like every other value (#151).
4040
- No longer calls the deprecated `curl_close()` (it was already a no-op on the supported PHP versions) (#149).
41+
- Auto-detection of the tracked page URL now uses `REQUEST_URI` as the source instead of `PATH_INFO`. With front-controller / path-info routing (e.g. `/dir1/page` handled by `dir1/index.php`), `PATH_INFO` only holds the trailing `/page`, so the tracker previously recorded a truncated URL; it now records the full requested path. `PATH_INFO` is no longer used at all (`SCRIPT_NAME` remains the fallback when `REQUEST_URI` is unavailable) (#141).
4142

4243
### Added
4344
- PHPStan static analysis at max level (`phpstan.neon.dist`) and the Matomo coding standard via PHP_CodeSniffer (`phpcs.xml.dist`), both enforced for every pull request through GitHub Actions.

MatomoTracker.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,24 +2621,26 @@ protected function getCookieMatchingName(string $name): string|false
26212621
}
26222622

26232623
/**
2624-
* If current URL is "http://example.org/dir1/dir2/index.php?param1=value1&param2=value2"
2625-
* will return "/dir1/dir2/index.php"
2624+
* Returns the path portion of the URL the visitor requested (everything between the host and
2625+
* the query string). For "http://example.org/dir1/dir2/index.php?param1=value1" this returns
2626+
* "/dir1/dir2/index.php"; for a front-controller URL such as "http://example.org/dir1/page"
2627+
* (where "/page" is handled by dir1/index.php) it returns "/dir1/page".
2628+
*
2629+
* The full request path is taken from REQUEST_URI. PATH_INFO is deliberately not used: it only
2630+
* holds the trailing path-info segment (e.g. "/page"), so it would drop the directory/script
2631+
* prefix and yield a truncated URL. SCRIPT_NAME is the fallback when REQUEST_URI is unavailable.
26262632
*
26272633
* @ignore
26282634
*/
26292635
protected static function getCurrentScriptName(): string
26302636
{
26312637
$url = '';
2632-
if (!empty($_SERVER['PATH_INFO'])) {
2633-
$url = self::toStringValue($_SERVER['PATH_INFO']);
2634-
} else {
2635-
if (!empty($_SERVER['REQUEST_URI'])) {
2636-
$requestUri = self::toStringValue($_SERVER['REQUEST_URI']);
2637-
if (($pos = strpos($requestUri, '?')) !== false) {
2638-
$url = substr($requestUri, 0, $pos);
2639-
} else {
2640-
$url = $requestUri;
2641-
}
2638+
if (!empty($_SERVER['REQUEST_URI'])) {
2639+
$requestUri = self::toStringValue($_SERVER['REQUEST_URI']);
2640+
if (($pos = strpos($requestUri, '?')) !== false) {
2641+
$url = substr($requestUri, 0, $pos);
2642+
} else {
2643+
$url = $requestUri;
26422644
}
26432645
}
26442646
if (empty($url) && isset($_SERVER['SCRIPT_NAME'])) {

tests/Unit/MatomoTrackerTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,17 +1848,24 @@ public function testGetCurrentScriptName(): void
18481848
unset($_SERVER['PATH_INFO'], $_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']);
18491849
$this->assertSame('/', TestableMatomoTracker::callGetCurrentScriptName());
18501850

1851+
// SCRIPT_NAME is only the fallback when REQUEST_URI is unavailable.
18511852
$_SERVER['SCRIPT_NAME'] = 'script.php';
18521853
$this->assertSame('/script.php', TestableMatomoTracker::callGetCurrentScriptName());
18531854

1855+
// REQUEST_URI is the primary source; the query string is stripped.
18541856
$_SERVER['REQUEST_URI'] = '/dir/page.php?query=1';
18551857
$this->assertSame('/dir/page.php', TestableMatomoTracker::callGetCurrentScriptName());
18561858

18571859
$_SERVER['REQUEST_URI'] = '/dir/other.php';
18581860
$this->assertSame('/dir/other.php', TestableMatomoTracker::callGetCurrentScriptName());
18591861

1860-
$_SERVER['PATH_INFO'] = '/path/info';
1861-
$this->assertSame('/path/info', TestableMatomoTracker::callGetCurrentScriptName());
1862+
// Front-controller / path-info routing: with a request for /dir1/page handled by
1863+
// dir1/index.php, PATH_INFO is only "/page". The full requested path must still be tracked,
1864+
// so REQUEST_URI wins and PATH_INFO is ignored (previously it truncated the URL to "/page").
1865+
$_SERVER['REQUEST_URI'] = '/dir1/page';
1866+
$_SERVER['PATH_INFO'] = '/page';
1867+
$_SERVER['SCRIPT_NAME'] = '/dir1/index.php';
1868+
$this->assertSame('/dir1/page', TestableMatomoTracker::callGetCurrentScriptName());
18621869
}
18631870

18641871
public function testGetCurrentQueryStringAndUrl(): void
@@ -1871,7 +1878,8 @@ public function testGetCurrentQueryStringAndUrl(): void
18711878

18721879
$_SERVER['HTTPS'] = 'on';
18731880
$_SERVER['HTTP_HOST'] = 'matomo.example';
1874-
$_SERVER['PATH_INFO'] = '/page';
1881+
unset($_SERVER['PATH_INFO']);
1882+
$_SERVER['REQUEST_URI'] = '/page';
18751883
$this->assertSame('https://matomo.example/page?a=b&c=d', TestableMatomoTracker::callGetCurrentUrl());
18761884
}
18771885

0 commit comments

Comments
 (0)