views:

67

answers:

3

How do I figure out in my PHP's code if I am in an iframe or not? I'd like the page to behave slightly differently. I was going to use HTTP_REFERER but that doesn't seem very reliable.

+2  A: 

You can't figure it out in PHP. PHP operates entirely on the server side, and there is no request header specifying whether a request is within an IFRAME or not AFAIK.

You would have to generate some JavaScript that finds out whether it is inside a frame, and sends the information back through AJAX. It's a bit complicated - you would have to generate a unique key for every request, and store that somewhere - but possible.

At that point, it's too late to influence the rendering of the page, of course.

Pekka
"too late to influence..." Not if you are ok with a bit of flicker.. You could have it build the entire page on the AJAX reply from the server which has all of the HTML for how the page should actually be rendered.... this is tricky at best though
Earlz
@Earlz good idea.
Pekka
Everything is possible these days..! :)
Lipis
True, true! :) Ajax, JQuery and the like really changed a lot in that respect.
Pekka
A: 

How different are your two cases?

If they're completely different, you could always send both to the client, hiding them with DIV tags until it's finished loading, then checking to see if (window.document == document) and showing the appropriate one.

If they're only slightly different, maybe only the styles being different, you could add some JavaScript to load the appropriate CSS file.

Raceimaztion
+1  A: 

If it's you, who decides (or loads) whether the script is called from IFRAME or not, you could call it with parameter, i.e. script.php?ff=1 - FromFrame and modify accordingly.

Adam Kiss
I like that, I should have thought of that.
ferik
And if this simplest solution fails, you can still spend sleepless nights playing with AJAX calls... :]
Adam Kiss