开发者

Detect iframe request in a rails app

开发者 https://www.devze.com 2023-03-25 11:00 出处:网络
I have a iFrame tool, which should get rendered in a other format than my page. So I want to detect requests from iFrame like I can 开发者_运维百科detect them from a iPhone. Is this possible?

I have a iFrame tool, which should get rendered in a other format than my page. So I want to detect requests from iFrame like I can 开发者_运维百科detect them from a iPhone. Is this possible?

Is there a special hint in the request header, that I could use, or could I manually enter one?

Thanks Markus


When you get a request from an iPhone there will be send a "User Agent"-String like Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 in the request.user_agent variable. Searching this string for iPhone can tell you that an iPhone is visiting your page.

An iframe is an HTML tag to include other pages within your page (eg. the Facebook like button uses this). This does not have to to anything with a specific browser, all modern browsers support this. You cannot tell on server side whether your page was called within an iframe or not. You can use some JavaScript in the client to find out if your current page is within an iframe and then send a notice to the server.

Best practice would be to add another paramter to your request like ?iframe=1 and use this param within your controller.


A good practice is to add a subdomain for your embeds. Then you can fire requests to the subdomain and evaluate request.host. This also allows for a separation of logic in controllers/views when using multiple subdomains.

Example:

iFrame

  • <iframe src="embed-subdomain.domain.com" /> instead of <iframe src="domain.com" />

Controller

  • iframe = request.host.include?("embed-subdomain.domain")

Disclaimer: This can get tricky if you utilize sessions and CNAMEs

0

精彩评论

暂无评论...
验证码 换一张
取 消