views:

760

answers:

2

Okay, here is my situation.

I have a page, index.php, which is the mainsite (flash site)

I have another page called iframe.php which contain iframe of index.php

Another page, test.php. Inside have 2 links, 1st link is directly to index.php, another link is to iframe.php

I tested:

  • I click the 1st link, when i trace/echo the HTTP_REFERER, it displays "test.php", but

  • I click on 2nd link, when i trace/echo the HTTP_REFERER, it displays "iframe.php".

Why it display "iframe.php"? Is HTTP_REFERER does not work on iframe??

Is there anyway to get the "test.php" when i click on second link?

Source code for :index.php

<html>
<head> // Some headers information
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">
var flashvars = {};
    <?php
if(!empty($_SERVER['HTTP_REFERER'])){
?>
    flashvars.link       =  '<?php echo $_SERVER['HTTP_REFERER']; ?>';
<?php
}
?>
var params = {};
var attributes = {};
swfobject.embedSWF("main.swf, "content", "100%", "100%", "9", "expressInstall.swf", flashvars, params, attributes);
</script>
</head>
<body>
    <div id="content">
    <a href="http://www.adobe.com/go/getflashplayer"&gt;
   <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
  </a>
    </div>
</body>
</html>

Source code for iframe.php

<html> headers tag
...
<body>
<center><iframe src="index.php" mce_src="index.php" frameborder="0" height="500" scrolling="no" width="500"></iframe></center>
</body>
</html>

Source code for test.php:

....
<a href="iframe.php" target="_blank">This is Iframe</a> <br><br>
....
<a href="index.php" target="_blank">This is normal link</a> <br><br>
+1  A: 

In either case you're seeing the output of index.php. Here's why:

Scenario 1)

When you hit index.php from the link in test.php, it loads index.php (with test.php as the HTTP_REFERER).

Scenario 2)

When you hit iframe.php from the link in test.php, it loads iframe.php which internally loads index.php in the <iframe> tag (with iframe.php as the HTTP_REFERER).

Asaph
Updated my question with the source code. Please take a look, see if there is anything wrong?
mysqllearner
Updated my answer
Asaph
No, the result is different. When i click on normal link, it display test.php(which is correct), but when i click iframe link, it displays iframe.php. Thats why I asking this question here. You have different result from me? May I know what OS and browser you are using?
mysqllearner
Btw, I am using Windows Vista, and firefox 3.5.5 as my internet browser
mysqllearner
Try to comment out the `<iframe>` tag in `iframe.php`. Now what happens?
Asaph
Comment out iframe tag? Its blank now, i viewed source code, its blank as well. May I know what did you get? I tried this: I put in different file. I mean, normal link in test1.php and iframe link is test2.php. Same result as mine, 1 displays test.php, another displays iframe.php. I afraid, paul answer is right, "The HTTP_REFERER value for a page shown inside an iframe will always be the parent page that contained the iframe.", could you confirm this?
mysqllearner
The reason I asked you to comment out the `<iframe>` was to prove to you that the output you were seeing when you hit `iframe.php` was in fact coming from `index.php` and being loaded via the `<iframe>` tag. Both @Paul and myself are telling you the same thing in different ways.
Asaph
Ahhhh... I got it. So I cant use http_referer on iframe in this case. Thanks for the answer. I cant thank you enough. I will get back to my client now :)
mysqllearner
A: 

Unfortunately, no. The HTTP_REFERER value for a page shown inside an iframe will always be the parent page that contained the iframe.

HTTP_REFERER tends to be a little tricky to count on anyway. If you can avoid building any important logic around it, it's a good idea to do so.

I gather you're using php -- perhaps you could use session to store the last page visited when test.php loads? On test.php, you set $_SESSION['referringPage'] = 'test.php';. Then on index.php, you read the value of $_SESSION['referringPage'], and you get the same information regardless of whether the page was loaded within the iframe.

Paul
Normally i use session. But this time, I cant do it. I will give my client only the link, example: <a href="iframe.php" target="_blank">This is Iframe</a>, I cant do much here. Or you have a better solution? My condition: give 1 link to client to detect previous URL.
mysqllearner
@Paul: The `$_SESSION` approach you recommend is bug prone when you have multiple browser windows/tabs open. Different requests can overwrite each other's session values.
Asaph
Ah.. I didnt notice that. But so far, I have not received any compaints yet. Gotta change the method asap. So, Asaph, whats the good way to do this? I mean get the previous URL??
mysqllearner