Dug*_*ugi 2 javascript php validation url hyperlink
我的图片主机有一个Google Chrome扩展程序,它会将URL发送到我的网站.该URL 通过Javascript的方法进行编码escape.
由以下内容编码的URL escape:
http%253A//4.bp.blogspot.com/-xa4Krfq2V6g/UF2K5XYv3kI/AAAAAAAAAJg/8wrqZQP9ru8/s1600/LuffyTimeSkip.png
Run Code Online (Sandbox Code Playgroud)
我需要通过PHP以某种方式将URL恢复正常,所以我可以检查它,filter_var($the_url, FILTER_VALIDATE_URL)如果URL如上所述,它显然会失败.
这就是Javascript的样子:
function upload(img) {
var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + escape(img.srcUrl);
uploadImage(baseurl);
}
function uploadImage(imgurl) {
chrome.tabs.create({
url: imgurl,
selected: true
});
}
var title = "Upload this image to IMGit";
var id = chrome.contextMenus.create({"title": title, "contexts": ['image'], "onclick": upload});
Run Code Online (Sandbox Code Playgroud)
这就是我在PHP中所做的:
if (!filter_var($file, FILTER_VALIDATE_URL) || !filter_var(urldecode($file), FILTER_VALIDATE_URL))
{
throw_error('The entered URLs do not have a valid URL format.', 'index.php#remote'); break;
}
Run Code Online (Sandbox Code Playgroud)
坦率地说,urldecode()不为我做这份工作.正如您所注意到的,我正在接收URL $_GET.
处理这种情况的最佳方法是什么?
实际问题:如何在PHP中取消转义的URL?有没有更好的方法来处理这个问题?
您将要使用encodeURIComponent而不是escape:
function upload(img) {
var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + encodeURIComponent(img.srcUrl);
uploadImage(baseurl);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以urldecode在PHP内部使用以获得所需的结果.
有关vs. vs. 的解释,请参阅此问题.escapeencodeURIencodeURIComponent