2017-09-17 25 views
1

我想保護我的內容免受example.php文件的影響。因此,我需要禁止直接訪問此頁面以及任何iframe指向未經我許可。如何讓我的PHP文件只在Iframe中加載

我配置一個名爲iframe.php的php文件,從example.com加載iframe。這是我如何做:

代碼使用example.php:

<?php 
session_start(); 
if(!isset($_SESSION['iframe']) || !isset($_GET['internal']) || $_SESSION['iframe'] != $_GET['internal']) 
{ 
    die("This page can be accessed just from within an iframe"); 
} 
unset($_SESSION['iframe']); 
?> 

代碼iframe.php

<?php 
session_start(); 
$_SESSION['iframe'] = md5(time()."random sentence"); 
?> 
<iframe src="example.php?internal=<?php echo $_SESSION['iframe'];?>" width="500" height="100"></iframe> 

它運作良好。但是,當我點擊iframe.php中的任何鏈接時,我將被重定向到其他頁面。之後,我單擊瀏覽器中的「後退」按鈕並收到消息:「此頁面只能從iframe中訪問」。

請你幫我保持頁面完好後回來?

謝謝您的閱讀。

+0

的[允許頁面只加載在iframe,防止直接訪問(可能的複製https://stackoverflow.com/questions/34197938/allow-a-page-to-only-load-in -an-iframe-and-prevent-direct-access) – thexpand

回答

0

其簡單,你可以通過Javascript來做到這一點。

if(window==window.top) { 
    // not in an iframe 
} 
+0

我已經嘗試過,但它仍然沒有工作。請注意,您的代碼不會阻止其他來源的iframe加載。 – Mike