<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Facebook Cookbook &#187; install</title>
	<atom:link href="http://fbcookbook.ofhas.in/tag/install/feed/" rel="self" type="application/rss+xml" />
	<link>http://fbcookbook.ofhas.in</link>
	<description>thousands of app development recipes</description>
	<lastBuildDate>Sun, 21 Mar 2010 10:38:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>how to keep track of users of your application, and how to remove them properly when they remove it</title>
		<link>http://fbcookbook.ofhas.in/2009/02/05/how-to-keep-track-of-users-of-your-application-and-how-to-remove-them-properly-when-they-remove-it/</link>
		<comments>http://fbcookbook.ofhas.in/2009/02/05/how-to-keep-track-of-users-of-your-application-and-how-to-remove-them-properly-when-they-remove-it/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 14:44:06 +0000</pubDate>
		<dc:creator>Hasin Hayder</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Application]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[uninstall]]></category>

		<guid isPermaLink="false">http://fbcookbook.ofhas.in/?p=108</guid>
		<description><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://fbcookbook.ofhas.in/2009/02/05/how-to-keep-track-of-users-of-your-application-and-how-to-remove-them-properly-when-they-remove-it/';
digg_bgcolor = '#FFFFFF';
digg_skin = '';
digg_window = '';
digg_title = 'how to keep track of users of your application, and how to remove them properly when they remove it';
digg_bodytext = '';
digg_media = 'news';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
there are two things we are going to discuss in this article. both of these are very important issues for successful marketing of your app, to generate precise traffic report. and if you want to sell your app or generate revenue, traffic report is very important. so we are going to show you how to [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;"><script type="text/javascript">
<!--
digg_url = 'http://fbcookbook.ofhas.in/2009/02/05/how-to-keep-track-of-users-of-your-application-and-how-to-remove-them-properly-when-they-remove-it/';
digg_bgcolor = '#FFFFFF';
digg_skin = '';
digg_window = '';
digg_title = 'how to keep track of users of your application, and how to remove them properly when they remove it';
digg_bodytext = '';
digg_media = 'news';
digg_topic = '';
//-->
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</div>
<p>there are two things we are going to discuss in this article. both of these are very important issues for successful marketing of your app, to generate precise traffic report. and if you want to sell your app or generate revenue, traffic report is very important. so we are going to show you how to maintain a precise list of your active users and how to precisely remove them when they remove your app. </p>
<p><b>keeping track of your users</b><br />
this one is fairly easy. but beside storing their user id you should keep track of their session ids. why? for extended permissions and for viral marketing. lets create a mysql table for storing these data, along with the active/inactive</p>
<pre name="code" class="sql">

+------------+---------------------+------+-----+---------+-------+
| Field      | Type                | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+-------+
| uid        | bigint(20) unsigned | NO   | PRI |         |       |
| sessionkey | varchar(128)        | YES  |     | NULL    |       |
| active     | char(1)             | YES  | MUL | 1       |       |
+------------+---------------------+------+-----+---------+-------+
</pre>
<p>here is the SQL</p>
<pre name="code" class="sql">

CREATE TABLE `users` (
  `uid` bigint(20) unsigned NOT NULL,
  `sessionkey` varchar(128) default NULL,
  `active` char(1) default &#039;1&#039;,
  PRIMARY KEY  (`uid`),
  KEY `activity` (`active`)
)
</pre>
<p>now can you remember that there is a field named &#8220;Post authorize redirect url&#8221; in the &#8220;Authentication&#8221; tab of your application settings page? set the data in that field before proceeding further. have a look at the following screenshot. you can see it&#8217;s enlarged view by clicking on it</p>
<p><a href="http://fbcookbook.ofhas.in/wp-content/uploads/2009/02/picture-7.png"><img src="http://fbcookbook.ofhas.in/wp-content/uploads/2009/02/picture-7-300x203.png" alt="picture-7" title="picture-7" width="300" height="203" class="alignnone size-medium wp-image-112" /></a></p>
<p>now lets see how to store the user ids and their corresponding session key. in the following code block there are two functions for storing user ids in your database and for updating their session key. </p>
<pre name="code" class="php">

&lt;?php
/**
 * insert a new user and corresponding session key in the users table
 *
 * @param int $uid user id
 * @param string $sk session key
 * @param resource $db mysql connection resource
 * @return bool
 */
function insertUser($uid,$sk,$db)
{
    $data= mysql_query(&quot;select uid from users where uid=&#039;{$uid}&#039;&quot;,$db);
    if(mysql_num_rows($data)==0)
    {
        $sql = &quot;INSERT INTO users (uid,sessionkey, active) VALUES(&#039;{$uid}&#039;,&#039;{$sk}&#039;,&#039;1&#039;)&quot;;
        mysql_query($sql,$db);
        return true;
    }
    return false;
}

/**
 * update an user&#039;s corresponding session key in users table
 *
 * @param int $uid user id
 * @param string $sk session key
 * @param resource $db mysql connection resource
 * @return bool
 */
function updateSessionKey($uid,$sk,$db)
{
   return mysql_query(&quot;update users set sk = &#039;{$sk}&#039; where uid=&#039;{$uid}&quot;,$db);
}

?&gt;
</pre>
<p>now in your <b>install.php</b> all you have to do is place the following code.</p>
<pre name="code" class="php">

include_once(&quot;config.php&quot;);
$sk = $facebook-&gt;api_client-&gt;session_key;

$db = mysql_connect(&#039;host&#039;,&#039;user&#039;,&#039;db&#039;);
mysql_select_db(&quot;db&quot;,$db);

insertUser($uid,$sk, $db);
echo &quot;&lt;fb:redirect url=&#039;YourApplicationUrl&#039; /&gt;&quot;;
</pre>
<p>thats it. now you can periodically update your user&#8217;s session key to stay uptodate using updateSessionKey() function. </p>
<p><b>how to remove your user</b><br />
remember in the previous screenshot there was a field named &#8220;Post-Remove Callback URL&#8221; in the application setup page? when any of your application users remove this application from their account, facebook sends some information to this callback url so that you, the application developer, can successfully take necessary steps to remove this user from your database or set the inactive flag &#8220;ON&#8221;. lets see how to do this. here is the code of &#8220;remove.php&#8221;</p>
<pre name="code" class="php">

&lt;?php
include_once(&quot;config.php&quot;);
$facebook = new Facebook($apikey, $secret);
$user = $facebook-&gt;get_loggedin_user();
if ($user != NULL &amp;amp;amp;&amp;amp;amp; $facebook-&gt;fb_params[&#039;uninstall&#039;] == 1)
{
    makeUserInactive($uid,$db); //$db is your mysql link indentifier
}

/**
 * set an user as inactive, indicating that user has removed your application
 *
 * @param int $uid user id
 * @param resource $db mysql connection resource
 * @return bool
 */
function makeUserInactive($uid,$db)
{
    return mysql_query(&quot;update users set active = &#039;0&#039; where uid=&#039;{$uid}&quot;,$db);
}
?&gt;
</pre>
<p>thats it! for reference you can check out <a target='_blank' href="http://wiki.developers.facebook.com/index.php/Post-Remove_URL">facebook wiki entry</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://fbcookbook.ofhas.in/2009/02/05/how-to-keep-track-of-users-of-your-application-and-how-to-remove-them-properly-when-they-remove-it/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
