2

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;
}
John
  • 473
  • 5
  • 20
  • 5
    It's the same server? Is there a reason you're using web access instead of figuring out the file system path and using `file_exists`? – Greg Schmidt Feb 10 '19 at 22:37

2 Answers2

4

@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'])) {
    ....
}
  • That did the trick, thanks. I realized I made a logic error elsewhere in my code when I originally tried to use file_exists. It was showing all files as existing even ones that weren't. That error is gone, now file_exists works properly. Thanks! – John Feb 10 '19 at 22:50
1

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).

ccKep
  • 5,786
  • 19
  • 31
  • Thanks, I was using get_headers because originally file_exists wasn't working for me because of a logic error elsewhere in my code. Now that that's been fixed, file_exists is working properly and I don't need to use get_headers as an alternate method. – John Feb 10 '19 at 22:51