|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61982 Provide encode/decode functions for RFC 4648 "base64url" format
Submitted: 2012-05-09 14:31 UTC Modified: 2012-06-24 22:42 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nospam at vyznev dot net Assigned:
Status: Duplicate Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: nospam at vyznev dot net
New email:
PHP Version: OS:


 [2012-05-09 14:31 UTC] nospam at vyznev dot net
Including Base64-encoded data in URLs and file names is a commonly encountered task.  The PHP base64_encode() function is not suitable for this, since its output may encode the characters "+" (which has special meaning in URL query parameters) and "/" (which is used as a path separator in file names and URL paths).

RFC 4648 ( defines, in section 5, a standard variant of Base64 encoding, called "base64url", for this purpose.  It differs from the usual "base64" format in that the characters "+" and "/" are replaced with "-" and "_" respectively, and that the use of the padding character "=" is optional and not usually recommended.

I would like to suggest that functions for encoding and decoding data in this format be provided in PHP alongside the existing base64_encode() and base64_decode() functions.  Although such functions can be relatively easily implemented in pure PHP code based on the existing Base64 encoding functions, a built-in implementation would presumably be both more efficient and more likely to be user correctly by script authors.

A sample PHP implementation of the proposed functions (which could be included in the documentation for backward compatibility purposes) is included below:

    function base64url_encode( $data, $pad = false ) {
        $base64 = strtr( base64_encode( $data ), '+/', '-_' );
        return ( $pad ? $base64 : rtrim( $base64, '=' ) );

    function base64url_decode( $base64, $strict = false ) {
        return base64_decode( strtr( $base64, '-_+/', '+/-_' ), $strict );


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-24 22:42 UTC]
This is a duplicate of
 [2012-06-24 22:42 UTC]
-Status: Open +Status: Duplicate
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Oct 18 17:03:34 2021 UTC