获取 Facebook 页面选项卡上的用户位置

发布于 2024-10-10 20:03:35 字数 357 浏览 0 评论 0原文

可口可乐似乎在这方面做得很好。即使在进行任何用户交互之前,他们也会设法知道您在他们的 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:

You're in Denmark, dude. Chill.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清引 2024-10-17 20:03:35

首先,我不认为可以使用 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.

ˉ厌 2024-10-17 20:03:35

通过 Facebook 的应用程序 API,我确信他们能够获取用户的 IP 地址,然后进行地理 IP 查找。或者 Facebook 提供了一个 api 调用来告诉他们哪个国家。


编辑:
来自 http://developers.facebook.com/docs/guides/canvas

为了创建个性化用户
体验,Facebook 会发送您的
有关的申请信息
用户。
此信息被传递到
使用 HTTP POST 的 Canvas URL
单个signed_request参数
其中包含一个base64url编码的
JSON 对象。

当用户首次访问您的应用时,
signed_request 参数包含
有限数量的用户数据
。在
为了获得所有用户的访问权限
您可获得的信息
默认情况下的应用程序(例如
用户的 Facebook ID),用户必须
授权您的申请。 Facebook
提供了多种不同的方式
处理授权范围从
登录按钮手动执行
您的 Web 服务器上的 OAuth2.0 流程。

<强>>包含区域设置和的 JSON 数组
当前用户的国家。这
区域设置和国家/地区始终
可用。

您获得的有关用户、其区域设置和国家/地区的信息数量有限,并且只有在他们授权该应用程序后您才能获得默认信息。

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

In order to create a personalized user
experience, Facebook sends your
application information about the
user.
This information is passed to
your Canvas URL using HTTP POST within
a single signed_request parameter
which contains a base64url encoded
JSON object.

When a user first accesses your app,
the signed_request parameter contains
a limited amount of user data
. In
order to gain access to all the user
information available to your
application by default (like the
user's Facebook ID), the user must
authorize your application. Facebook
offers a number of different ways to
handle authorization ranging from the
Login Button to manually performing
the OAuth2.0 flow on your Web server.

> A JSON array containing the locale and
country of the current user. The
locale and country are always
available.

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.

嘿嘿嘿 2024-10-17 20:03:35

轻松找到此信息

您可以通过 PHP $country = $_POST['fb_sig_country']

使用自定义选项卡的开发人员可以利用我们新的 *fb_sig_country* 请求标头根据用户的区域设置显示不同的内容。例如,如果您的应用程序使用自定义选项卡来显示最新的世界杯排名,则可以使用 fb_sig_country 来显示用户所在国家/地区的亮点。 (注意:如果您使用fb_sig_country,您还必须处理该字段为空的情况,因为您可能没有世界上每个国家/地区的内容。)

来自 http://developers.facebook.com/blog/post/394

You can find this very simply via PHP

$country = $_POST['fb_sig_country']

Developers who use custom tabs can leverage our new *fb_sig_country* request header to display different content based on a user’s locale. For example, if your application uses a custom tab to show the latest World Cup standings, you can use fb_sig_country to present highlights for a user’s country. (Note: if you use fb_sig_country, you must also handle the case where the field is blank, because you might not have content for every country in the world.)

via http://developers.facebook.com/blog/post/394

水水月牙 2024-10-17 20:03:35

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文