获取 Facebook 页面选项卡上的用户位置
可口可乐似乎在这方面做得很好。即使在进行任何用户交互之前,他们也会设法知道您在他们的 Facebook 页面< /a>:
这适用于 IE,因此不涉及 HTML5(之后所有,Facebook 无法识别 Javascript 中的 navigator
对象)。
他们是如何做到这一点的?
Coca-Cola seems to have nailed it pretty well. Even before any user interaction, they manage to know where you are at their Facebook page:
This works in IE, so no HTML5 involved (and after all, Facebook won't recognize the navigator
object in Javascript).
How do they do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我不认为可以使用 javascript 来完成。下面介绍了如何使用服务器端语言(例如 PHP)来完成此操作。
如果您仔细查看您提供的页面(可口可乐登陆选项卡),您会发现即使用户未使用 Facebook 登录,国家/地区的选择仍然有效。
当您设置 Facebook 应用程序时,您可以提供选项卡 URL。这是相对于您的画布 URL 的地址,当您的应用程序添加到页面(选项卡内)时,Facebook 将从该地址提供内容。
如果您对 $_SERVER 超全局变量执行 var_dump,您将看到一个名为
HTTP_X_FB_USER_REMOTE_ADDR
的变量。这是相应用户的 IP 地址。获得用户 IP 地址后,您可以轻松使用地理定位服务来查找国家/地区:)。祝你好运!
稍后编辑
看来我正在讨论的变量在选项卡内不可用。我刚刚做了一些更多的测试,无论用户是否登录,您总是有一个名为
fb_sig_country
的 POST 变量,它是用户 cmes 所在国家/地区的 TLD。First of all, I don't think it can be done using javascript. Here's how you can do it using a server side language, like PHP.
If you take a closer look of the page you provided (coca cola landing tab) you can see that selection of the country works even if the user is not logged in with facebook.
When you set up a facebook application you can provide a tab url. This is the address relative to you canvas url from which facebook serves the content when your application is added to a page (inside a tab).
If you do a var_dump of the $_SERVER superglobal variable you would see a variable named
HTTP_X_FB_USER_REMOTE_ADDR
. This is the IP address of the respective user. After you get the user IP address you can easily use a geolocation service to find out the country :).Good luck!
later edit
It seems that the variable i was talking about is not available inside a tab. I just did some more tests and either the user is logged in or not you always have the POST variable called
fb_sig_country
which is the TLD of the country the user cmes from.通过 Facebook 的应用程序 API,我确信他们能够获取用户的 IP 地址,然后进行地理 IP 查找。或者 Facebook 提供了一个 api 调用来告诉他们哪个国家。
编辑:
来自 http://developers.facebook.com/docs/guides/canvas
您获得的有关用户、其区域设置和国家/地区的信息数量有限,并且只有在他们授权该应用程序后您才能获得默认信息。
Through Facebook's app api i am sure they are able to get the ip address of the user and then do a geo ip lookup. Or facebook provides an api call that tells them what country.
EDIT:
From http://developers.facebook.com/docs/guides/canvas
You get a limited amount of information about the user, their locale and country, and only until they authorize the app can you get the default information.
轻松找到此信息
您可以通过 PHP
$country = $_POST['fb_sig_country']
来自 http://developers.facebook.com/blog/post/394
You can find this very simply via PHP
$country = $_POST['fb_sig_country']
via http://developers.facebook.com/blog/post/394
Facebook 在签名请求的用户部分中传递当前用户的国家/地区和区域设置。请阅读此处:https://developers.facebook.com/docs/authentication/signed_request/
如果您有 iframe 应用程序,也可以使用普通的 geo-ip 方法(现在是必需的,所以您很可能会这样做)。 MaxMind 提供基于 javascript 的检测以及在服务器端使用的数据库。如果您在应用程序中的某个位置包含返回 maxmind.com 的链接,它是免费的。 http://www.maxmind.com/app/javascript_city
Facebook passes the current user's country and locale in the user portion of the signed request. Read up here: https://developers.facebook.com/docs/authentication/signed_request/
It's also possible to use normal geo-ip methods if you have an iframe application (which is now required, so most likely you do). MaxMind provides javascript-based detection as well as databases for use on the server side. It's free if you include a link back to maxmind.com somewhere in your app. http://www.maxmind.com/app/javascript_city