Cookie cant be read even cookie is present

The $_COOKIE array contains only these cookies that came in the request from the browser.

setcookie() adds an HTTP header asking the client to send the cookie during subsequent requests.

So yeah – you can’t access $_COOKIE in the same request it is set.