Search code examples
javascriptcloudflareweb-workergeoip

Get Cloudflare's HTTP_CF_IPCOUNTRY header with javascript?


There are many SO questions how to get http headers with javascript, but for some reason they don't show up HTTP_CF_IPCOUNTRY header.

If I try to do with php echo $_SERVER["HTTP_CF_IPCOUNTRY"];, it works, so CF is working just fine.

Is it possible to get this header with javascript?


Solution

  • Assuming you are talking about client side JavaScript: no, it isn't possible.

    1. The browser makes an HTTP request to the server.
    2. The server notices what IP address the request came from
    3. The server looks up that IP address in a database and finds the matching country
    4. The server passes that country to PHP

    The data never even goes near the browser.

    For JavaScript to access it, you would need to read it with server side code and then put it in a response back to the browser.