Hi,

I have tried to find out on this, but I cannot be sure.

Anyone knows for certain fopen and file_get_contents (or any similar functions) will generate an entry in the target domain's website traffic stats?

I know this could be traffic analyser specific, so for a start, will it appear in webalizer, awstats or google analytics. if it does, will it appear as a regular user with the server IP or another form, like a bot or something with identifiable IP?

Thanks

Dani AI

Generated

Brief recap and clarification (for , following ): server-side PHP fetches that use an HTTP URL (fopen/file_get_contents/cURL) generate real HTTP requests that the remote webserver answers, so they show up in server access logs and in any analytics that parse those logs (Webalizer, AWStats, etc.). They will not trigger client-side JavaScript analytics (Google Analytics) because GA runs in a browser; to record server-side activity in GA you must send hits explicitly (Measurement Protocol).

What the remote log records

  • The entry will reflect whatever IP the request actually came from (your server's outgoing IP, or a proxy/NAT IP), the request line, HTTP status, bytes, and any headers you sent (User-Agent, Referer). Log-based tools will attribute the hit accordingly and may classify it as a robot if the User-Agent matches known bots.
  • If allow_url_fopen is disabled, wrappers won’t work and no HTTP request will be made; cURL still works independently. For details on PHP wrappers and stream contexts see the PHP manual.

Quick examples to control how the request appears

  • Use a stream context to add headers for file_get_contents:
    $opts = [
    'http' => [
      'method'  => "GET",
      'header'  => "User-Agent: MyScript/1.0\r\nReferer: https://example.com/\r\n"
    ]
    ];
    $ctx = stream_context_create($opts);
    $content = file_get_contents('http://target.example/path', false, $ctx);
  • With cURL you can set similar options:
    $ch = curl_init('http://target.example/path');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'MyScript/1.0');
    $content = curl_exec($ch);
    curl_close($ch);

Practical checks and cautions

Recommended Answers

All 2 Replies

Well when using file_get_contents a request is sent to the remote server with the User Agent as "PHP" which can be configured in php.ini. I believe the same holds true for cURL but a cURL request can be configured to customize the user agent. So yes, it does get logged like a normal request.

So yes, it does get logged like a normal request.

thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.