How do you set a php cookie with multiple values?

后端 未结 4 1074
面向向阳花
面向向阳花 2020-12-18 04:37

I want to create a php cookie that stores the username and the userid. Or is it better to just use one to get the other?

4条回答
  •  眼角桃花
    2020-12-18 04:51

    If you're only looking to store two values, it may just be easier to concatenate them and store it as such:

    setcookie("acookie", $username . "," . $userid);
    

    And to retrieve the information later,

    if(isset($_COOKIE["acookie"])){
        $pieces = explode(",", $_COOKIE["acookie"]);
        $username = $pieces[0];
        $userid = $pieces[1];
    }
    

    Cheers,

    ~Berserkguard

提交回复
热议问题