为了演示,让我简化一下URL:
http://yoursite.com/iframe_load.html?url=http://theirsite.com/index.php?a=1&b=2
请注意,他们网站的URL也包含查询字符串(index.php?a=1&b=2
)。因为URL包含&
,所以PHP会将字符串分割成以下两部分:
url=http://theirsite.com/index.php?a=1
&
b=2
可以看到,url
现在只包含URL的一部分,而不是完整的URL(因为它被&
分隔开了)。
&
符号过早“断开”了URL,因此您必须用不会中断URL的内容来替换它。
您必须传递一个编码后的URL给iframe_load.html,以防止PHP误解URL:
http://yoursite.com/iframe_load.html?url=http%3A%2F%2Ftheirsite.com%2Findex.php%3Fkey1%3Dval1%26key2%3Dval2
(注意,iframe_load.html?url=
后面的部分不再包含任何?
或&
)
如果您从另一个页面链接到iframe_load.html,您可以使用PHP的urlencode()
函数来为您完成此操作:
链接到iframe_load.html的某些页面
<?php
$other_websites_url = 'http://theirsite.com/index.php?key1=val1&key2=val2';
?>
<a href="http://yoursite.com/iframe_load.html?url=<?php echo urlencode($other_websites_url); ?>">Click to go to iframe_load.html</a>
<?php
使用
urlencode()
,URL中的特殊字符(如
&
)将被改为HTML实体,因此它们(暂时)失去了意义,不会破坏URL。不用担心,在访问
iframe_load.html
时,URL将被解码,所以您将得到正确的iframe URL。
这是他们网站编码后的URL示例:
http%3A%2F%2Ftheirsite.com%2Findex.php%3Fkey1%3Dval1%26key2%3Dval2
如您所见,
&
已被替换为
%26
,其他字符也进行了替换。现在,您只需将其粘贴到您的URL即可:
http://yoursite.com/iframe_load.html?url=http%3A%2F%2Ftheirsite.com%2Findex.php%3Fkey1%3Dval1%26key2%3Dval2
因为其他网站的URL不再包含
&
,PHP不会分割URL。
iframe_load.html?url=
后面的 URL 应该已经被编码。这样,iframe_load.html 就不会将 URL 分成几个部分。 - Nic Wortel