'file_put_contents causes page to refresh

I am calling a file_put_contents() inside of a PHP script that is executed via AJAX. Whenever I execute this script the client page gets reloaded which is not desirable. Is there a way to prevent the PHP script from reloading the page?

I have tried modifying the headers using the following and passing it as the context parameter in the file_put_contents function:

$context = stream_context_create(
    array(
        'http' => array(
            'follow_location' => false
        )
    )
  );


Solution 1:[1]

Try runnning script in incognito mode. Had same problem with file_put_contents(), and it was live reload VSCode extension that for some reason was refreshing the page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Voj