我有一个表格希望嵌入到我的网站中,该网站在我的白名单中。
其他试图嵌入该表格的网站将只会获得错误页面。
<iframe src="https://domain.tld/getForm.php?embed=1&formId=123456"></iframe>
我希望能够在getForm.php
中使用$_SERVER['HTTP_REFERER']
来检查嵌入网站,但这并没有起作用。
有人知道最佳实践或任何解决方法吗?
提前致谢!
内容安全策略头现在是推荐的方法。
来自 MDN 的示例:
// iframe can be embedded in pages on the origin and also on https://www.example.org
Content-Security-Policy: frame-ancestors 'self' https://www.example.org;
更多详情请参见:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
大多数浏览器都支持 X-Frame-Options header。
此标头将防止访问:
X-Frame-Options: SAMEORIGIN
并且添加以下标头以允许访问:
X-Frame-Options: ALLOW-FROM [uri]
选项的示例:
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
X-Frame-Options: ALLOW-FROM https://example.com/
PHP中的一个例子:
<?php header('X-Frame-Options: SAMEORIGIN'); ?>
你可以在这里进一步阅读:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
希望对你有所帮助!
allow-from
现在已经过时。 - codesmithContent-Security-Policy: frame-ancestors 'self' yoursites;
来保护你的网站。 - Mukesh Kumar Gupta
getForm.php
的远程地址的IP,但是IP可能会被伪造。查看$_SERVER
超级全局变量。我认为REMOTE_ADDR
应该是发起调用的站点的IP,但我不确定是否只是客户端的IP。 - ArtisticPhoenix