Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
How to pass json encode into cookie in PHP

When im check JSON encode with print_r it look fine => {"expiry":1636191448,"auth":true}

Then im try to store it inside cookie, but it turn into like this when i see it in browser cookies value => %7B%22expiry%22%3A1636191866%2C%22auth%22%3Atrue%7D

enter image description here

Can anyone tell me how to fix it. Im not use this for permission system, but for other thing like read it in javascript but i got error when try to parse it

Uncaught SyntaxError: Unexpected token % in JSON at position 0 at JSON.parse ()

.

This are the line of code in my php file

if (Cookie::exists(Config::get('client/cookie_name'))) {
    $client = json_decode(Cookie::get(Config::get('client/cookie_name')), true);
    $expiry = $client['expiry'];
} else {
    $client = array (
        'expiry' => time() + Config::get('client/cookie_expiry')
    );
}

$client['auth'] = true;

$client = json_encode($client);
Cookie::update(Config::get('client/cookie_name'), $client, $expiry);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Cookies are often URL-encoded (but sometimes different encodings are used). Since this isn't really standardized and by spec there is only a handful of characters that must be encoded somehow (not specified how), different systems may take different routes in handling cookie encoding, and therefore reading cookies set by one system using another system can cause issues like these.

In your case, you can call decodeURIComponent on the value first:

const json = decodeURIComponent(encodedCookieValue)
const data = JSON.parse(json) 

Further reading: Should cookie values be URL encoded?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda