如果一个网页是example.com网站上的iframe,它会显示一些数据而不是整页

时间:2016-01-16 15:35:44

标签: javascript php html iframe

我想只在我的网站(mydomain.com)的iframe页面中打开网站(example.com)。在iframe中,它应显示与iframe中链接的原始页面不同的内容。

<?php
$url="http://example.com";
if (stripos($_SERVER['REQUEST_URI'], '$url'))
{
echo 'This content gose on iframe page only for http://example.com ';
else {
echo "This content for main web page (mydomain.com) and other websites where this webpage is iframed ";
}
?>

我添加了显示我想要编码的示例代码?

1 个答案:

答案 0 :(得分:2)

这里有一些问题。 (请参阅下面的编辑)。

首先,变量不会用单引号解析。

if (stripos($_SERVER['REQUEST_URI'], "$url"))

或者只是删除它们

if (stripos($_SERVER['REQUEST_URI'], $url))

此条件语句还缺少右括号}

if (stripos($_SERVER['REQUEST_URI'], '$url'))
{
$url="http://example.com";
if (stripos($_SERVER['REQUEST_URI'], $url))
{
echo 'This content gose on iframe page only for http://example.com ';
} // This one was missing
else {
echo "This content for main web page (mydomain.com) and other websites where this webpage is iframed ";
}

并且仅此一点就会让你对它产生影响:

  

解析错误:语法错误,第x行的/path/to/file.php中出现意外的'else'(T_ELSE)

已设置错误报告。

修改

然而:

$_SERVER['REQUEST_URI']会给你:

/some-dir/yourpage.php

我怀疑你想在这里使用,或者应该使用,因为它会失败。

您可能想要使用:

$url = strtolower($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);

if (

($url=="www.example.com/file.xxx") 

||

($url=="example.com/file.xxx") )

{
echo 'This content gose on iframe page only for http://example.com ';
}
else {
echo "This content for main web page (mydomain.com) and other websites where this webpage is iframed ";
}

N.B。:我无需在上面的编辑中添加http://,因为它会自动填充。

您也可以使用:
(注意:请勿添加http://http://www.或其他任何内容,仅添加服务器名称。

if (strpos($_SERVER['SERVER_NAME'], "example.com") !== false)

如果要检查它是否来自特定文件夹/文件,可以使用另一种方法:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strrpos($url, "http://www.example.com/folder/file.php") !== false)

请注意,!== false的使用在这里非常重要,如果您要将其更改为=== true将会失败,以便尝试检查真相并给出误报,所以不要使用=== true

另请注意,http://www.example.comhttp://example.com不一样。您需要特别使用符合标准的那个。