I want to pass a variable set by the user to the setcookie function. I would like to let user change the color of some parts of website. so far the information about color is sent to server with $_SESSION['colorcode'] and I would like to add it to setcookie as well so when the user logs in to the site next time, his/her color is there. I’ve got this code:
setcookie( 'colorcode', $_SESSION['colorcode'], time() + 60 * 60 * 24 * 30, '', '', false, true );
I would like to save the value of variable in cookie, but it works just for the session. what is wrong? how to do it so the color is there when the user logs in? I’m looking for another way than storing it in database or file.
Did you read back the value from the cookie at the beginning of the next session? Setting the cookie looks good but I think the last parameters could be omitted.
Perhaps even the path (
'/') is optional. But this only sets the cookie. You have to read the data back in, when the user returns to your site the next time.When there is no
colorcodein the session but the cookie-value exists, then the data is validated and if it’s a valid 6 digit hex color code, then the value is inserted into the session. The validation is nessessary because a cookie is data that comes from the user and therefore potentially malicious.