iFrame加载不正常

3
我正在使用一个iframe加载页面。这是我使用的代码:
文件:iframe_load.html
<?php echo 'URL = '.$_REQUEST['url']; ?>
<iframe width="1015px" id="article_frame" src="<?php echo $_REQUEST['url']; ?>" style="border:0 none;"  height="576px" sandbox="allow-scripts"></iframe>

如果我在浏览器中访问此网址:http://mywebsite.com/iframe_load.html?url=http://www.northjersey.com/r?19=961&43=515347&44=234450391&32=4497&7=309037&40=http%3A%2F%2Fwww.northjersey.com%2Fnews%2FSecret_bunker_under_Prague_hotel_opens_to_public_.html&nid=4306612,那么 $_REQUEST['url'] 只会显示部分数值。这样我的 iframe 就无法显示实际的页面内容。请帮我解决此问题。
4个回答

2

为了演示,让我简化一下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会将字符串分割成以下两部分:

  1. url=http://theirsite.com/index.php?a=1 &
  2. 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
// create a variable with the URL
// this is the normal URL, we will encode it later, when we echo it.
$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 //                                                     ^ see, here we urlencode the URL, just before we paste it inside our own URL.

使用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。

使用**urlencode()**函数后,我得到的URL链接如下:URL = http%3A%2F%2Fwww.northjersey.com%2Fr%3F19%3D961。 - ARUN
你需要在链接到iframe_load.html时使用它,而不是在iframe_load.html内部使用。 - Nic Wortel
换句话说,当您访问 iframe_load.html 时,iframe_load.html?url= 后面的 URL 应该已经被编码。这样,iframe_load.html 就不会将 URL 分成几个部分。 - Nic Wortel

0
请按照以下方式进行:
$datum = parse_url($_SERVER['REQUEST_URI']);   /*use this if you want to get current page path*/
$test = "http://mywebsite.com/iframe_load.html?url=http://www.northjersey.com/r?19=961&43=515347&44=234450391&32=4497&7=309037&40=http%3A%2F%2Fwww.northjersey.com%2Fnews%2FSecret_bunker_under_Prague_hotel_opens_to_public_.html&nid=4306612";
$datum = parse_url($test);
print_R($datum['query']);

它只打印 19=961 - ARUN
@ARUN 我尝试使用字符串 $test。请再次检查,不要使用 url_encode()。 - Parixit
@ARUN 请复制我的代码并执行它。根据您的需求进行微调。 - Parixit
如果我按照您的代码进行复制,那么它是可以工作的。但是,如果我像这样替换$test的值 $test = $_REQUEST['url'];,那么它就无法工作了。 - ARUN
@ARUN请不要使用$_REQUEST ['url']。请使用我在代码中展示的第一行。 - Parixit

0
这是一个简单的解决方案:
对URL变量后面的部分进行URL编码,而不是整个URL,您需要在实际生成URL的位置进行更改,而不是在解析URL的位置进行更改:
$url_param_val = urlencode("http://www.northjersey.com/r?19=961&43=515347&44=234450391&32=4497&7=309037&40=http%3A%2F%2Fwww.northjersey.com%2Fnews%2FSecret_bunker_under_Prague_hotel_opens_to_public_.html&nid=4306612");

$new_url = "http://mywebsite.com/iframe_load.html?url=".$url_param_val;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接