doulangbi6869 2015-02-03 04:09
浏览 78

在cookieid上的PHP字符串操作

Currently on my webpage, im showing my users their cookieid, which has two parts associated to it, known as their partyid and userid,

the way it is set

localhost/setcookie.php?ptyid=1&uid=628929

result

1%3A628929

the way it is shown

echo 'Cookie Set => 
PartyID : ' .substr($_COOKIE["cookieid"],-8,1). ', UserID : ' .substr($_COOKIE["cookieid"],2);

this was going fine till my setcookie.php only allowed the users to set partyID as 1 and userID had to have exactly 6 characters in it. but now i have to allow them to use any partyid and any amount of characters to the userID.

Therefore is there anyway I could use a substring(or any other method) dynamically to serve any partyid or userid. i think of something which could detect the %3A and split the string but I fail to find a way for that.

  • 写回答

2条回答 默认 最新

  • dongxun7962 2015-02-03 04:36
    关注

    When storing the cookie, concatenate the parts by a predefined separator (can be single or multiple characters). Then you can use preg_split to split the parts instead of substr.

    This requires the parts them self must not contain the separator. You can make it a constraint for users. Or you can base64_encode the parts to make sure they will only contain certain characters.

    评论

报告相同问题?