Premium url 更短的问题 urlencode replacing & sign with & &符号

Premium url shortner issue with urlencode replacing & sign with ampersand

作为初学者 php 学习者,我正在使用 Code-Canyon Premium URL Shortner 脚本并进行了 2 天的研究。很遗憾,我无法解决我的问题。

url 缩短脚本是 url 编码它发送给脚本的 API url,这样做是替换 &带有 & 的符号导致 url 在最终目标页面上无法正常工作。

我曾尝试在目标页面上使用 preg_replacestr_replace 并尝试使用 urldecode,但其中 none 似乎有效。这是我当前的脚本:

$makeshort = "http://mywebsite.com/email/quote.php?quoteid=$visitor&customertype=fhbs";
$mkshrt = str_replace("/&/","%26",$makeshort);
$short = "http://shorturl.com/api?&api=REMOVED&format=text&url=".urlencode($mkshrt);

// Using Plain Text Response
$api_url = $short;
$res= @file_get_contents($api_url);
if($res)
$shorturl = $res;
$shorty = json_decode($shorturl);
$shorturl = $shorty->{'short'};
echo $shorturl;

Note: Where you see &format=text in the api url, I have tried to use it with and without the &format=text however this makes no difference what so ever.

我希望有一个简单快捷的方法来解决这个问题,因为我只传递了 2 个变量,第二个变量显示如下:

mywebsite.com/email/quote.php?quoteid=01234567890&customertype=fhbs

所以 customertype 变量是由于 amp; 符号而被弄乱的变量。

我真诚地希望有专业知识的人能给我建议最好的方法,甚至是解决这个问题的简单方法,因为我真的已经筋疲力尽了!我的知识不够好,无法研究确切的关键短语以指明正确的方向。

感谢您花时间阅读本文,我希望有人会好心帮助我。

我知道这种感觉,因为我自己正在适应编码和开发。

我个人会通过以下两种方法之一解决这个问题,如果您已经尝试将 htmlspecialchars 或 htmlentities 与 urldecode 一起使用,那么实现此目的最简单快捷的方法是阅读 URL 字符串然后使用 str_replace 将 & 符号替换为 & 并执行页面元刷新或`header 位置重定向

我的意思是用一个简短的例子,但是必须强调可能需要一些额外的安全性,这只是一个快速修复,而不是一个安全稳定和永久的修复,虽然可以玩这个,也许可以解决一些问题看你自己的情况。

$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
    $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $url = str_replace('&', '&', $url);
    echo "<meta http-equiv='refresh' content='0;URL=$url'>";
    exit;
}

header 位置的替代方式:

$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&amp;")){
    $url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $url = str_replace('&amp;', '&', $url);
    header("Location: $url");
    exit();
}

这将从 url 中完全删除任何 &amp; 个符号,并将它们替换为 &。 您还可以使用它来从 url 字符串中删除更多内容并替换 / 或禁止使用的词。

输出示例如下所示:

原来的url导致的问题:

http://mywebsite.com/email/quote.php?quoteid=1234567890&amp;客户类型=fhbs

脚本执行并刷新页面后新建url:

http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs

正如您从上面的超链接文本中看到的那样,和号打断了字符串,之后的所有内容都无法正确读取,但是当此脚本执行并刷新页面时,url 就像第二个超链接一样从而使 url 满足您的需求。

注意:这不是一种安全的处理方式,可能不适合您的情况,这只是一个想法,希望对您有所帮助!

谢谢。