PHP code example of germania-kg / base64-coder

1. Go to this page and download the library: Download germania-kg/base64-coder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

germania-kg / base64-coder example snippets



use Germania\Base64Coder\Base64Coder;
use Germania\Base64Coder\Exceptions\EncodingException;
use Germania\Base64Coder\Exceptions\DecodingException;

// Optional:
$logger    = new Monolog;
$separator = "::" // Default;

// Setup
$coder = new Base64Coder( $separator, $logger );
$coder = new Base64Coder( $separator );
$coder = new Base64Coder;


$selector = "user_john";
$token    = "somerandomvalue";

try {
	$encoded = $coder->encode( $selector, $token);
	// Result is something like
	// "RGllcyBpc3ftvZ::GllcmVuZGVyIFN0cmluZw=="
} 
catch (EncodingException $e) {
	// PHP's base64_encode had returned FALSE
}


$encoded_value = 'RGllcyBpc3ftvZ::GllcmVuZGVyIFN0cmluZw==';

try {
	$decoded = $cookie_coder->decode( $encoded_value );

	// Result object:
	echo $decoded->selector; // "user_john"
	echo $decoded->token;    // "somerandomvalue"
	
catch (DecodingException $e) {
	// PHP's base64_decode had returned FALSE
}



use Germania\Base64Coder\Exceptions\CoderExceptionInterface;

class EncodingException implements CoderExceptionInterface {}
class DecodingException implements CoderExceptionInterface {}


use Germania\Base64Coder\Providers\PimpleServiceProvider;
use Germania\Base64Coder\EncoderCallable;
use Germania\Base64Coder\DecoderCallable;
use Germania\Base64Coder\Base64Coder;
use Psr\Log\LoggerInterface;

// have your Pimple DIC ready, and optionally a PSR3 Logger:
$sp = new PimpleServiceProvider;
$custom_separator = "::";
$sp = new PimpleServiceProvider($separator, $psr3_logger);

$sp->register( $dic );

// Grab your services;
// See also above examaples.
$base64coder = $dic[Base64Coder::class];

$encoder = $dic['Cookie.Encryptor']; // Deprecated
$encoder = $dic[EncoderCallable::class];
$encoded_string = $encoder("selector", "token");

$decoder = $dic['Cookie.Decryptor']; // Deprecated
$decoder = $dic[DecoderCallable::class];
$decoded_obj = $decoder( $encoded_string );

echo $decoded_obj->selector;
echo $decoded_obj->token;