We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to properly use urlencode and urldecode in Phalcon?

Hi I need to encrypt the data sent by URL, using base64 but had trouble decrypting. I want to use encodeURL And DECODEURL properly considering special characters and spaces

edited Jun '16

What you need first is to provide more details. You use GET http method for data exchange, where data is encrypted in a binary form and encoded with base64, and what exact issue do you have?

$encoded = urlencode( base64_encode( $str ) );
$decoded = base64_decode( urldecode( $encoded ) );
edited Jun '16

Here are simple functions that i use to transfer encoded information. It is url safe.

private static $cryptKey = 'i$a^&/:%2_z10ROQ<@{(e=*!<7u|rI~0';

public static function encodeString($string)
{
    $crypt = new \Phalcon\Crypt();
    return $crypt->encryptBase64($string, self::$cryptKey, true);
}

public static function decodeString($string)
{
    $crypt = new \Phalcon\Crypt();
    return $crypt->decryptBase64($string, self::$cryptKey, true);
}

And example of course:

$string = 'Hello it is me Nikolay';

$encoded = \Helpers\Tools::encodeString($string);
echo $encoded; // EfLYhEcnsGUiXZ8LZ30uH2DuZMfVmcmuDuZm-tG8w1QO2zDPhkiDf315J11v001OeEJAjS6CwiDB7Ve8B_zp7g==

$decoded = \Helpers\Tools::decodeString($encoded);
echo $decoded; // Hello it is me Nikolay
edited Jun '16

You can also avoid base64, and use hexadecimal notation/convert both binary and text data and send it via HTTP GET.

  //Before send
        $str = 'This is binary data or \0 terminated array of bytes.';
        //convert it to hexadecimal
        $hex = bin2hex($str); //send $hex via HTTP GET method
// ###########################################
        //After fetch
        $decodeHex = pack('H*', $hex); //or simply $_GET['data'] instead of my test $hex variable

I use this approach while connecting to mainframe upstream services which does not support base64 or json :)

Hexadecimal notation is supported on any platform out there.



81.2k

In the same php file it has always worked well. The problem is when e-url special characters

Here are simple functions that i use to transfer encoded information. It is url safe.

private static $cryptKey = 'i$a^&/:%2_z10ROQ<@{(e=*!<7u|rI~0';

public static function encodeString($string)
{
   $crypt = new \Phalcon\Crypt();
   return $crypt->encryptBase64($string, self::$cryptKey, true);
}

public static function decodeString($string)
{
   $crypt = new \Phalcon\Crypt();
   return $crypt->decryptBase64($string, self::$cryptKey, true);
}

And example of course:

$string = 'Hello it is me Nikolay';

$encoded = \Helpers\Tools::encodeString($string);
echo $encoded; // EfLYhEcnsGUiXZ8LZ30uH2DuZMfVmcmuDuZm-tG8w1QO2zDPhkiDf315J11v001OeEJAjS6CwiDB7Ve8B_zp7g==

$decoded = \Helpers\Tools::decodeString($encoded);
echo $decoded; // Hello it is me Nikolay


81.2k

To me they helped me I run this

function base64url_encode($data) {
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

function base64url_decode($data) {
    return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}

@jeiel if you generate the string with my functions you will not have problems to send/receive via URL. The only difference to yours is that it is a bit more secure.