简体   繁体   中英

PHP get raw cookie

Maybe this is a duplicate question but I couldn't find it.

The thing is I have a stored cookie in the browser like username=test+1@test.com and when I read it from the php $_COOKIE["username"] I get test 1@test.com . I want to have the cookie without url decoding, just the way the browser is sending it. I know that there exist the funciton setrawcookie() and I want something like the reverse of that (like getrawcookie ).

Thanks

Based on dubafek's comment I built this to get raw cookie values:

$raw_cookies = (array_reduce(
    explode(';', $_SERVER['HTTP_COOKIE'] ?? ''),
    function($carry, $item) {
        $pair_arr = explode('=', $item);
        $carry[ trim($pair_arr[0]) ] = trim($pair_arr[1]);
        return $carry;
    },
    []
) ?: []);

From there you can use $raw_cookies as you would $_COOKIE . I needed to have the raw values when forwarding cookies in a curl call.

Edit: There are minor differences between the keys of $raw_cookies and $_COOKIE . For instance, a cookie with key connect.sid will appear in $_COOKIE['connect_sid'] and $raw_cookies['connect.sid'] . I am not sure what transformations PHP is doing to cookie keys so those may or may not need to be accounted for in your particular implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM