Warm tip: This article is reproduced from serverfault.com, please click

How to send correct translation in cookie value in php

发布于 2020-11-30 15:38:23

I have a basic cookie that is incorrectly encoding (or incorrectly decoding) the URL characters.

The cookie will set, however the value isn't encoding properly. Cookie value needs to be: https://mywebsite.com, however once I set the cookie, it displays the value as: https%3A%2F%2Fmywebsite.com, so the cookie doesn't work.

All the other variables are working, but I cannot get encoding to write https://

Cookie is in php (wordpress).

Code is:

<?php

//Cookie
$cookie_name = "cookie";
$cookie_value = "https://mywebsite.com";
setcookie($cookie_name, $cookie_value, time() + (86400), "/", ".mywebsite.com", TRUE); 
//end cookie

?>

I've tried adding urldecode($cookie_name); and urldecode($cookie_value); but neither works.

Questioner
RigelVII
Viewed
11
GrumbleShark 2020-12-01 00:35:22

You should look at setrawcookie

setrawcookie — Send a cookie without urlencoding the cookie value

setrawcookie() is exactly the same as setcookie() except that the cookie value will not be automatically urlencoded when sent to the browser.

Your code would look like

<?php

//Cookie
$cookie_name = "cookie";
$cookie_value = "https://mywebsite.com";
setrawcookie($cookie_name, $cookie_value, time() + (86400), "/", ".mywebsite.com", TRUE); 
//end cookie

?>