Detect iframe request in a rails app

我是研究僧i 提交于 2019-12-23 09:48:39

问题


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


回答1:


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.




回答2:


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")


来源:https://stackoverflow.com/questions/6908813/detect-iframe-request-in-a-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!