O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting and Setting Cookies

Problem

I need to get or set cookies for my application.

Solution

Use the Data.setCookie() method to set a cookie (where user is the UID of the user you want to drop the cookie for):

$cookieReturn = $facebook->api_client->data_setCookie($user,
 'cookie_type', 'chocolate_chip');

Use the Data.getCookies() method to get a cookie (where user is the UID of the user you want to retrieve the cookie for):

$cookie = $facebook->api_client->data_getCookies($user, 'cookie_type');

Discussion

You can specify optional expires and path settings for cookies when you drop them (the defaults are 24 hours and /, respectively):

$cookieReturn = $facebook->api_client->data_setCookie($user,
 'cookie_type', 'chocolate_chip', '1254079800', '/some/path/here');

Note that the expires argument is measured in epoch seconds (see Formatting Relative Time for more information about epoch time).

If you don’t specify a cookie name for Data.getCookies(), it will return all of the available cookies for the specified user.

Cookies in Facebook are handled differently than a regular browser-side cookie dropped by a regular web page. The cookies are actually managed by Platform and are associated with the user’s account rather than residing in her browser, so they’ll be present whenever that user logs into Facebook from any computer. Facebook will pass any unexpired cookies to your application with each request from a Canvas page to your callback URL, and will store any cookies you return (up to a limit of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required