Need help with PHP URL encoding/decoding
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
EDIT:
I solved 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);
精彩评论