A cookie is a piece of data that is stored in a browser and associated with a website. Every time the browser requests something from a website, it includes the cookies in the request headers.
You can read and write cookies with client side JavaScript. (NB: It is possible to mark cookies as http_only, in which case they can't be read with client side JS).
You can read (by examining the request headers) and write (using response headers) cookies with server side code.
The usual rules of timing apply:
show<?php echo $keys; ?><?php setcookie(
Setting a cookie with server side code requires you to issue a response header. PHP doesn't let you issue response headers after you have started outputting the response body.
You are trying to call setcookie
too late. You need to call it before you start outputting any content.
"document.write(current)"
I'm not sure what you are trying to achieve here.
- It doesn't make much sense to store JavaScript code in a cookie.
- You can't generate server side code using client side code, so if this is an attempt to write the value of
current
into a cookie, then it is too late.
setcookie("bgx","document.write(current)");
echo $_COOKIE["bgx"];
$_COOKIE
is populated with data from the request.
It won't have data in it that your current response is about to ask the client to store. You won't get that new value until the next request.
In short: To read a cookie set with JavaScript, you have to make a new HTTP request after you have set it.