需要 PHP URL 编码/解码方面的帮助
在一个页面上,我“屏蔽”/编码传递到另一个页面的 URL,在那里我解码 URL 并开始将文件传递给用户。
我发现了一些用于编码/解码 URL 的函数,但有时编码的 URL 包含“+”或“/”,并且解码的链接已损坏。
我必须使用“文件夹结构”进行链接,不能使用QueryString!
这是编码函数:
$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>';
这是解码函数:
$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;
那么如何对url进行编码和解码。 谢谢
编辑:
我用 str_replace 解决了:)
编码时:
$result = str_replace('%2B', '-', $result);
$result = str_replace('%2F', '_', $result);
解码时:
$urll = str_replace('-', '%2B', $urll);
$urll = str_replace('_', '%2F', $urll);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论