PHP: Checking if file exists, but @get_headers affecting tracker

853 Views Asked by At

I'm using PHP to check if an .html file exists on the server. However, @get_headers seems to be "visiting" the page when it checks for the file, and my tracking script that produces an analytics report is picking that up as a page view. Is there another way to check if the file exists without that happening? Here's the code I'm using now:

$file = "https://www." . $_SERVER['HTTP_HOST'] . $row['page'];
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $file_exists = false;
}
else {
    $file_exists = true;
}
2

There are 2 best solutions below

1
On BEST ANSWER

@get_headers seems to be "visiting" the page when it checks for the file

That's exactly what it's doing, yes.

Is there another way to check if the file exists without that happening?

By checking whether the file exists. Right now, what you're checking is "whether the URL returns an error when requested".

If you don't have any special URL rewrites in place, you could probably do this with:

if (file_exists($_SERVER["DOCUMENT_ROOT"] . $row['page'])) {
    ....
}
1
On

If you really need to use get_headers you might find Example #2 in the docs helpful.

In short: get_header by default uses GET requests (which, by all means - is a page view).

Example #2 for reference:

<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
$headers = get_headers('http://example.com');
?>

Although I prefer not changing the default stream context, so I'd actually suggest creating your own:

<?php
$context = stream_context_create(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);

$headers = get_headers('http://example.com', 0, $context);
?>

Wether this works or not mostly depends on your analytics software (ie wether it differentiates between GET and HEAD requests).