Need help coding / decoding php urls

On one page, I “mask” / encode the URL that is passed to another page, there I decode the URL and start delivering the file to the user.

I found some function for the encoding / decoding url, but the once-encoded url contains "+" or "/" and the decoded link is broken.

I have to use the "folder structure" for the link, I can not use QueryString!

Here is the encoding function:

$urll       = 'SomeUrl.zip';
$key        = '123'; 
$result     = '';


for($i=0; $i<strlen($urll); $i++) {
     $char = substr($urll, $i, 1);
     $keychar = substr($key, ($i % strlen($key))-1, 1);
     $char = chr(ord($char)+ord($keychar));
     $result.=$char;
    }
$result = urlencode(base64_encode($result));
echo '<a href="/user/download/'.$result.'/">PC</a>';

Here is the decoding:

$urll       = 'segment_3'; //Don't worry for this one its CMS retrieving 3rd "folder"
        $key        = '123'; 
        $resultt    = '';
        $string     = '';

        $string     = base64_decode(urldecode($urll));


        for($i=0; $i<strlen($string); $i++) {
             $char = substr($string, $i, 1);
             $keychar = substr($key, ($i % strlen($key))-1, 1);
             $char = chr(ord($char)-ord($keychar));
             $resultt.=$char;
            }

        echo '<br />DEC: '. $resultt;

So how to encode and decode a URL. Thanks


EDIT:

I decided with str_replace :)

When encoding:

$result     = str_replace('%2B', '-', $result);
$result     = str_replace('%2F', '_', $result);

When decoding:

$urll       = str_replace('-', '%2B', $urll);
$urll       = str_replace('_', '%2F', $urll);
+2
source share

Source: https://habr.com/ru/post/1761826/


All Articles