Sam Kingston
Sam Kingston

Reputation: 877

Check to see if website is loaded via Iframe

I have index.php as follows:

<html>
<head>
<title>Page Title</title>
<meta name="description" content="Description">
<meta name="keywords" content="Keyword1, Keyword2">
</head>
<body style="margin:0px;padding:0px;overflow:hidden">
<iframe id="myID" src="/index2.php" frameborder="0" style="overflow:hidden;height:100%;width:100%" >
</iframe>
</body>
</html>

The iframe in index.php is used to mask index2.php (i.e. iframe src). The user can however simply enter the website through

www.website.com/index2.php 

which would undermine all masking. Is there a way to know via javascript or other means whether iframe is loaded on each of the website's pages and redirect user if they have entered the site via index2.php.

Upvotes: 0

Views: 1507

Answers (1)

Geoff Atkins
Geoff Atkins

Reputation: 1703

I don't think there's a way you can do this reliably. You either need to use JavaScript within the iframe page to test if the document is in the top frame, or use PHP to check the referrer.

Place one of the following in the page you want loaded inside the iframe.

PHP

if ($_SERVER['HTTP_REFERER'] != 'http://domain.com/index.php') {
header('Location: http://domain.com/index.php');
}

JavaScript

if (window==window.top) { 
window.location = "http://domain.com/index.php"
}

NB
However, since JavaScript can be turned off, and the referrer can be spoofed, it's never going to be entirely foolproof.

Possibly the best remedy would be to replace the iframe with an include and update it using AJAX.

Upvotes: 4

Related Questions