How to send correct translation in cookie value in php

Viewed 46

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.

1 Answers

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

?>
Related