Place an Array in a PHP cookie

Asked

Viewed 1,624 times

1

I have a query that returns the accesses of the user who just logged in. I need to turn them into an array and store them in a session and a cookie. Follow code:

//ARMAZENA AS PERMISSOES DO PERFIL DO USUARIO
if($sql_permissoes->rowCount() > 0){
    $permissao_visualizacao = array();
    $permissao_alteracao        = array();
    foreach ($sql_permissoes as $sql_permissoes_row) {
        $permissao_visualizacao[]   = $sql_permissoes_row["url_localizacao"];
        $permissao_alteracao[]      = $sql_permissoes_row["permite_alteracao"];
    }
}

$_SESSION["ged.permissao.visualizacao"] = $permissao_visualizacao;
$_SESSION["ged.permissao.alteracao"]    = $permissao_alteracao;
setcookie("ged.permissao.visualizacao", $permissao_visualizacao, mktime(0, 0, 0, date("m"), date("d")+5, date("Y")) );
setcookie("ged.permissao.alteracao", $permissao_alteracao, mktime(0, 0, 0, date("m"), date("d")+5, date("Y")) );

But in the setcookie lines I get the following message:

Warning: setcookie() expects parameter 2 to be string, array given.

What is the most recommended method of doing this operation?

  • It might work, but remember browsers may have limitations on storage size, maybe it’s best to store in server sessions and use the cookie only to identify which session is whose (the same session_* already does this).

3 answers

1

A good alternative is to serialize the array, that is, transform it into a string: http://php.net/manual/en/function.serialize.php

setcookie("ged.permissao.visualizacao", serialize($permissao_visualizacao), mktime(0, 0, 0, date("m"), date("d")+5, date("Y")) );
setcookie("ged.permissao.alteracao", serialize($permissao_alteracao), mktime(0, 0, 0, date("m"), date("d")+5, date("Y")) );

To read the cookie you must use the unserialize: http://php.net/manual/en/function.unserialize.php

$permissao_visualizacao = unserialize($_COOKIE['ged_permissao_visualizacao']);
$permissao_alteracao = unserialize($_COOKIE['ged_permissao_visualizacao']);

I hope I’ve helped.

  • Using the serialize stopped giving error in setcookie, but while trying to recover it with the unserialize the error appears: Notice: Undefined index: ged.permissao.visualizacao. It is worth remembering that I did the test right after the cookie was created and also after page redirection, both with the same return.

  • 1

    @Dirtyoldman, I did the test here and I don’t know the reason, but the setcookie swaps the points for underscore, give a var_dump($_COOKIE) and see how your cookie was saved. response edited.

0

As the error suggests, cookie data should be in string format. Most importantly, you should not store actual data in a cookie because the user can edit it.

$permissao_visualizacao = unserialize($_COOKIE['ged.permissao.visualizacao']);

session_start() should be called before anything is output on each page.

if the error persists try to use a third parameter in the set cookie setcookie('email', $first_email, time()+60*60*24*100);

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.