difference between setcookie and $_COOKIE in php
cookies, php
Solution
You can't actually "set" a cookie with some code like this:
`$_COOKIE['cookie'] = $my_var;`
All this does is add a new value to the `$_COOKIE` array. No `Set-Cookie` HTTP header is sent back to the client (browser) in the response and no cookie will be created on the client.
Use the `setcookie()` function to set cookies.
The current accepted answer correctly points out that `$_COOKIE` is set/initialized at the start of the PHP process and isn't updated after that. You can update it yourself but don't expect that value to stick on the next request.
Problem
Is there any difference between setting a cookie via `setcookie()` and `$_COOKIE` ? Sometimes,when setting a cookie via `setcookie`,i don't get the value of that cookie via $_COOKIE['cookie_name'].But js console.log immediately after `setcookie`,shows that cookie is set but if i try to get the value of the cookie via $_COOKIE,i don't get the updated value. I'm confused..!!