Need help with PHP URL encoding/decoding

Posted by Kenan on Stack Overflow See other posts from Stack Overflow or by Kenan
Published on 2010-04-14T13:00:03Z Indexed on 2010/04/14 13:03 UTC
Read the original article Hit count: 229

Filed under:
|
|

On one page I'm "masking"/encoding URL which is passed to another page, there I decode URL and start file delivering to user.

I found some function for encoding/decoding URL's, but sometime encoded URL contains "+" or "/" and decoded link is broken.

I must use "folder structure" for link, can not use QueryString!

Here is 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 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 url. Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about encode