Set a facebook session app from access token

In some situation you will want to create a facebook session for your apps, for example you have a CODE
variable from facebook after user granting your application and then you fetch the access token like this way below,

<pre lang="php">$accessToken = file_get_contents("

A session apps need these variables included (uid, access_token, and sig) I believe that you already had the latest facebook PHP-SDK, “sig” is a signature for your cookie base on your App Secret, your need to create session variables by manually and then set that variables with “setSession” method on PHP-SDK.

These are functions that might help you,

<pre lang="php">//Build your session variables
function generateSessionVars($accessToken)
$e = explode('|',$accessToken);
$s = explode('-',$e[1]);

$params = array(
'access_token' => trim($accessToken)
$params['sig'] = generateSignature($params,APP_SECRET);

return $params;
//Generate session signature base on you App Secret
function generateSignature($params,$secret) {

// work with sorted data

// generate the base string
$base_string = '';
foreach($params as $key => $value) {
$base_string .= $key . '=' . $value;
$base_string .= $secret;

return md5($base_string);

Wrap it out,

<pre lang="php">//get session variables
$params = generateSessionVars($access_token);

If cookie created succesfully your PHP-SDK library could identify with getSession() method if session
is set or not.

Khalid Adisendjaja

Internet tech wanderer. Networking enthusiast. Radio Announcer. Casual Gamer. OpenSource Fans. Information Junkies. My tweet is my own.

Jakarta, Indonesia