php.net |  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
Votes:1
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nospam at vyznev dot net
New email:
PHP Version: OS:

 

 [2012-05-09 14:31 UTC] nospam at vyznev dot net
Description:
------------
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 (http://tools.ietf.org/html/rfc4648) 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 );
    }



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-24 22:42 UTC] nikic@php.net
This is a duplicate of https://bugs.php.net/bug.php?id=33650.
 [2012-06-24 22:42 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 02:01:35 2025 UTC