【问题标题】:PHP Creating new variable for each time a link in an iframe changesPHP为每次iframe中的链接更改时创建新变量
【发布时间】:2014-05-11 14:26:45
【问题描述】:

我有一个基本的 html 表单页面,用户在其中输入一个单词,然后提交到一个带有 iframe 的 php 页面,然后在 wikipedia 中搜索该术语,如下所示:

<?php

$userEntered = $_POST["userEntered"];

echo
'<html xmlns="http://www.w3.org/1999/xhtml" lang="EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Full Page IFrame</title>
<style type="text/css">
html {overflow: auto;}
html, body, div, iframe {margin: 0px; padding: 0px; height: 100%; border: none;}
iframe {display: block; width: 100%; border: none; overflow-y: auto; overflow-x: hidden;}
</style>
</head>
<body>
<iframe id="tree" name="tree" src="http://en.wikipedia.org/wiki/' . $userEntered . '" frameborder="0" marginheight="0" marginwidth="0" width="100%" height="100%" scrolling="auto"></iframe>
</body>
</html>';
?>

我想知道每次用户点击维基百科页面上的链接时如何创建一个新变量。

即使您导航到 iframe 中的新维基百科页面,$userEntered 变量似乎也保持不变。

【问题讨论】:

  • 在表单中输入一个文本字段,然后提交并设置 target= iframe
  • iframe 内容是完全独立的网页。在其中单击的链接不会影响包含页面。除非他们有 target="_top" 或一些 JS 代码来突破框架。

标签: php html iframe


【解决方案1】:

这是你想要的工作(测试代码)

<?php
$userEntered = !empty($_POST["userEntered"])?$_POST["userEntered"]:'test';

echo
'<html xmlns="http://www.w3.org/1999/xhtml" lang="EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Full Page IFrame</title>
<style type="text/css">
html {overflow: auto;}
html, body, div, iframe {margin: 0px; padding: 0px; height: 100%; border: none;}
iframe {display: block; width: 100%; border: none; overflow-y: auto; overflow-x: hidden;}
</style>
</head>
<body>
<form method="POST" >
<input type="text" name="userEntered">
<input type="submit" name="submit">

</form>
<iframe id="tree" name="tree" src="http://en.wikipedia.org/wiki/' . $userEntered . '" frameborder="0" marginheight="0" marginwidth="0" width="100%" height="100%" scrolling="auto"></iframe>

</body>
</html>';

?>

【讨论】:

    【解决方案2】:

    这是不可能的(除非您可以控制维基百科域)。

    您无法检索 iframe 的 URL,这是一件好事。如果有可能,任何人都可以设置网页并从 URL 中检索敏感信息。

    这是same origin policy

    另外,PHP 是一种服务器端语言,它只是生成页面并传递它,之后发生的事情不是 PHP 关心的。使用 javascript 检测页面中的更改。

    编辑:不存在同源问题的情况的解决方案

    要在更改时将 iframe 的 URL 传递给 PHP,您必须使用 javascript 获取并发送 URL。

    使用 jQuery 的简单示例:

    $(function() {
        $('#tree').load(function() {
            var frameUrl = $('#tree').contents().get(0).location.href;
            $.post("backend.php", {url: frameUrl});
        });
    });
    
    <?php
    
    // Make sure it exists and filter it if necessary.
    $url = $_POST['url'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 2016-02-15
      • 2020-08-08
      • 2015-01-01
      • 2016-01-27
      相关资源
      最近更新 更多