PHP URL Encoding / Decoding

前端 未结 4 593
生来不讨喜
生来不讨喜 2020-11-29 12:59

I used the solution accepted for this question for encrypting by id for example in /index.php?id=3 . The problem is I cannot send the encrypted value as an

4条回答
  •  不知归路
    2020-11-29 13:55

    If you use Base64 to encode the binary value for the URL, there is also a variant with URL and filename safe alphabet.

    You can use the strtr function to translate one from alphabet to the other:

    $base64url = strtr($base64, '+/', '-_');
    $base64 = strtr($base64url, '-_', '+/');
    

    So you can use these functions to encode and decode base64url:

    function base64url_encode($str) {
        return strtr(base64_encode($str), '+/', '-_'));
    }
    function base64url_decode($base64url) {
        return base64_decode(strtr($base64url, '-_', '+/'));
    }
    

    See also my answer on What is a good way to produce an short alphanumeric string from a long md5 hash?

提交回复
热议问题