A virtual teacher who reveals to you the great secrets of Base64

PHP Base64 Encode

To encode data in Base64, PHP provides a function called base64_encode. The inverse transformation is achieved by using the function base64_decode. Unlike data decoding, the encoding function does not conceal any pitfalls.

Usage:

  • base64_encode($data) - Encodes $data to Base64 and returns value as STRING

Arguments:

  • $data (required string) - Text or binary content that you want to encode

Return Values:

  • STRING - On success, it returns the Base64 value, typically longer by about 33% than $data
  • FALSE - If a failure occurs (if $data cannot be encoded to Base64), it returns boolean FALSE

Supported Versions:

  • PHP 4
  • PHP 5
  • PHP 7

Example #1 (print the result):

<?php
$str = 'guru';
echo base64_encode($str); //-> "Z3VydQ=="

Example #2 (store the result in a variable and check for errors):

<?php
$str = 'Hello World!!';
$b64 = base64_encode($str);

if ($b64 === false) {
  echo 'Invalid input';
} else {
  echo $b64; //-> "SGVsbG8gV29ybGQhIQ=="
}

Example #3 (invalid type of $data triggers E_WARNING “base64_encode() expects parameter 1 to be string, array given”):

<?php
echo base64_encode([]); //-> FALSE

For more info, check the following examples:

Comments (16)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • nqhXncMU,
    -1" OR 2+391-391-1=0+0+0+1 --
  • nqhXncMU,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    555*39*34*0
  • nqhXncMU,
    wJML861p' OR 712=(SELECT 712 FROM PG_SLEEP(15))--
  • nqhXncMU,
    555*145*140*0
  • nqhXncMU,
    555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • gBqsPxAZ,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • nqhXncMU,
    -1); waitfor delay '0:0:15' --
  • gBqsPxAZ,
    if(now()=sysdate(),sleep(15),0)
  • nqhXncMU,
    555*66*61*0
  • nqhXncMU,
    if(now()=sysdate(),sleep(15),0)
  • nqhXncMU,
    -1)) OR 155=(SELECT 155 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    -5 OR 896=(SELECT 896 FROM PG_SLEEP(15))--
  • nqhXncMU,
    if(now()=sysdate(),sleep(15),0)
  • ncMUFCMU,
    -1); waitfor delay '0:0:15' --
  • ncMUFCMU,
    -1); waitfor delay '0:0:15' --
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.