Custom Header Not Available in PHP

I have tried to replicate Cloudflare’s CF_IPCOUNTRY functionality for a visitors’ timezone in a worker.

I can see the timezone in the response header on Chrome’s network tab e.g. America/New_York. I have tried many ways but try as I might I can not get that response header to show on my PHP page. Any ideas?

------- PHP Code -----------

echo $_SERVER[“HTTP_CF_IPCOUNTRY”]; // Works e.g. US
echo $_SERVER[“HTTP_X_TIMEZONE”]; // Shows nothing

------- CF Worker ------------

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {

const response = await fetch(request)
const newResponse = new Response(response.body, response)
newResponse.headers.append(“X_TIMEZONE”, request.cf.timezone)

return newResponse
}

You are adding that to the response header, but on the server side you need to access the request header. In other words, you are adding it to the wrong location, that needs to go to the request headers.

4 Likes

Thanks Sandro! That was just the pointer I needed. Below is working for me now.

async function handleRequest(request) {

let headers = new Headers()
headers.append(“X-TIMEZONE”, request.cf.timezone)
return fetch(request, {headers: headers})
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.