<?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>nFriedly Web Dev Tech Blog &#187; cookie</title>
	<atom:link href="http://nfriedly.com/techblog/tag/cookie/feed/" rel="self" type="application/rss+xml" />
	<link>http://nfriedly.com/techblog</link>
	<description>Expert Advice on Website Development, Javascript, Ajax, and Security</description>
	<lastBuildDate>Wed, 16 May 2012 23:48:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How Facebook sets and uses cross-domain cookies</title>
		<link>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/</link>
		<comments>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 16:08:06 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=310</guid>
		<description><![CDATA[I&#8217;ve seen a lot of confusion about this lately, so I thought I&#8217;d make a quick writeup to explain how facebook does it. (I&#8217;ll also give a quick tip on how you can do it yourself.) What Facebook Does Facebook is in a unique position compared to many developers looking to set cross domain cookies: [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" src="http://nfriedly.com/img/portfolio/facebook.png" alt="" />I&#8217;ve seen a lot of confusion about this lately, so I thought I&#8217;d make a quick writeup to explain how facebook does it. (I&#8217;ll also give a quick tip on how you can do it yourself.)</p>
<h3>What Facebook Does</h3>
<p>Facebook is in a unique position compared to many developers looking to set cross domain cookies: The user visits both facebook.com and the other website. <span id="more-310"></span></p>
<p>Facebook never actually sets cookies cross-domain, they only read cookies cross-domain. They set cookies on facebook.com when the user visits facebook.com and they set cookies on the other  example.com (or any other website) when the user visits example.com.<sup><a href="#note-1">1</a></sup></p>
<p>Doing things this way avoids all of the browser security issues because cookies that were already set when the user visited facebook.com can still be read when example.com loads facebook.com in an iframe. This is worth repeating: <strong>Cookies can be read in an iframe if they were set outside of the iframe</strong>.<sup><a href="#note-2">2</a></sup></p>
<h3>What about when the user is not logged into Facebook?</h3>
<p><strong>(This is how you can do it!)</strong></p>
<p>If the user is not logged into Facebook when trying to use Facebook on example.com, then Facebook opens a popup window &#8211; not an iframe &#8211; to let the user log in.</p>
<p>A popup window has none of the cookie restrictions that iframes get; it can read and set cookies normally.</p>
<h3>What about popup blockers?</h3>
<p>Most popup blockers make a special exception for &#8220;intentional&#8221; popups &#8211; ones that occur as a direct result of a user&#8217;s click. When the user clicks the login button, the blocker allows the popup because the click indicates that the user wanted that popup.</p>
<h3>An alternate method for of cross-domain cookies: flash</h3>
<p>If you&#8217;re looking for a flash-based method of setting cross-domain cookies, or would otherwise like to avoid popups, you may want to check out my previous article, which includes source code: <a href="http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/">.swf for JavaScript cross-domain flash cookies</a></p>
<h3>Notes</h3>
<ol>
<li><a name="note-1"></a>Cookies are only set on example.com when using the using <a href="http://developers.facebook.com/docs/reference/javascript/">Facebook&#8217;s JavaScript SDK</a>. When embedding Social plugins directly as an iframe, only facebook.com cookies are used.</li>
<li><a name="note-2"></a>Safari sometimes prevents JavaScript from reading cookies in an iframe even if GET and POST requests to the server have full access to the cookies. Safari has several quirks like this, but generally <a href="http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/">behaves better with iframes if the user <em>interacts</em> with it</a>.</li>
</ol>
<h2>Need a more advanced integration than what Facebook Social Plugins provide?</h2>
<p>At <a href="http://sociablelabs.com">Sociable Labs</a>, our Intelligent Social Plugins<sup>TM</sup> increase social sharing by 15x and have shown a ~1% increase in sales. And the best part is that <a href="http://www.sociablelabs.com/product.html">we do all of the hard work for you!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript library and .swf for cross-domain flash cookies</title>
		<link>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/</link>
		<comments>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 18:25:11 +0000</pubDate>
		<dc:creator>nFriedly</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[actionscript 3.0]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[cross-domain]]></category>
		<category><![CDATA[externalinterface]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flashcookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[sharedobject]]></category>
		<category><![CDATA[swf]]></category>

		<guid isPermaLink="false">http://nfriedly.com/techblog/?p=272</guid>
		<description><![CDATA[I&#8217;m working on a project that has a legitimate (non-spammy) reason to need cross-domain cookies, and we settled on flash as a good way to accomplish this. However, I was surprisingly unable to find any complete library or how-to guide for connecting flash cookies to javascript. So I dusted off my flash skills and built [...]]]></description>
			<content:encoded><![CDATA[<div style="position:relative;">
<img class="alignright size-full wp-image-295" title="325990_chocolate_chip_cookies_2" src="http://nfriedly.com/techblog/wp-content/uploads/2010/07/325990_chocolate_chip_cookies_2.jpg" alt="" width="300" height="224" />I&#8217;m working on a project that has a legitimate (non-spammy) reason to need cross-domain cookies, and we settled on flash as a good way to accomplish this.</p>
<p>However, I was surprisingly unable to find any complete library or how-to guide for connecting flash cookies to javascript. So I dusted off my flash skills and built one, and and now you get to enjoy the fruit of my labor:</p>
<p><a href="http://github.com/nfriedly/Javascript-Flash-Cookies"><img style="position: absolute; top: 0pt; right: 0pt; border: 0pt none;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
</div>
<p><span id="more-272"></span></p>
<p><a href="http://github.com/nfriedly/Javascript-Flash-Cookies/zipball/master"><img class="alignleft" src="/img/application_put.png" alt="" /> Download the .swf, .js, and source code from github</a></p>
<p>This is an .swf file  that communicates with JavaScript via flash&#8217;s <a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html">ExternalInerface</a> to read and write to a Local <a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html">SharedObject</a> (LSO). Essentially, it&#8217;s cross-domain cookies for javascript.</p>
<p>It also includes an (optional) javascript library that handles embedding, communication, error checking, and logging.</p>
<p>The project is hosted at github: <a href="http://github.com/nfriedly/Javascript-Flash-Cookies">http://github.com/nfriedly/Javascript-Flash-Cookies</a></p>
<hr style="margin-top: 20px;" />
<p><em>You might also be interested in <a href="http://nfriedly.com/techblog/2010/08/how-facebook-sets-and-uses-cross-domain-cookies/">How Facebook Sets and uses cross-Domain cookies</a></em></p>
<hr />
<h3>Working Example</h3>
<p>See <a href="http://nfriedly.com/stuff/swfstore-example/">https://nfriedly.com/stuff/swfstore-example/</a> and <a href="http://nfriedly.github.com/Javascript-Flash-Cookies/">http://nfriedly.github.com/Javascript-Flash-Cookies/</a> for a working example. </p>
<h3>Quick start guide</h3>
<p>To use the library, upload the storage.swf &amp; swfstore.js files to your web server and put this HTML and JavaScript into your web page(s):</p>
<h4>The HTML</h4>
<pre class="brush: xml; title: ; notranslate">
&lt;!-- This example uses jquery, but SwfStore does not require jquery to work. --&gt;
&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/PATH/TO/swfstore.js&quot;&gt;&lt;/script&gt;

&lt;input id=&quot;dataInput&quot; /&gt; &lt;input id=&quot;saveBtn&quot; type=&quot;submit&quot; value=&quot;Save&quot; /&gt;

&lt;div id=&quot;status&quot;&gt;&lt;/div&gt;
</pre>
<h4>And The JavaScript</h4>
<pre class="brush: jscript; title: ; notranslate">

// wait until the page has finished loading before starting
$(function(){

	// first disable things while the swfStore is initializing
	$('input').attr(&quot;disabled&quot;,&quot;disabled&quot;);
	$('#status').text('Loading...');

	var mySwfStore = new SwfStore({

		// Optional but recommended. Try to choose something unique.
		namespace: 'myExample', 

		// To work cross-domain, only one of your sites should have the
		// .swf, all other sites should load it from the first one
		swf_url: 'http://site.com/PATH/TO/storage.swf', 

		// Logs messages to the console if available, a div at the
		// bottom of the page otherwise.
		debug: true,

		onready: function(){
			// Now that the swfStore was loaded successfully, re-enable
			$('input').removeAttr(&quot;disabled&quot;);

			// Read the existing value (if any)
			$('#dataInput').val(mySwfStore.get('myKey'));

			// Set up an onclick handler to save the text to the
			// swfStore whenever the Save button is clicked
			$('#saveBtn').click(function(){
				mySwfStore.set('myKey', $('#dataInput').val() );
				$('#status').text('Saved!')
			});

			$('#status').text('Loaded');
		},

		onerror: function(){
			// In case we had an error. (The most common cause is that
			// the user disabled flash cookies.)
			$('#status').text('Error');
		}
	});
});
</pre>
<h3>Cross-domain usage</h3>
<p>A copy of storage.swf located on one domain may be embedded on pages from one or more other domains, allowing cross-domain cookie access.</p>
<style>
.highlight { background-color: #D1FCA9; }
</style>
<h3>Troubleshooting</h3>
<ul>
<li>Be sure the <span class="highlight">urls</span> to the .swf file and .js file are both correct.</li>
<li>If the .swf file is unable to communicate with the JavaScript, it will <span  class="highlight">display log messages on the flash object</span>. If debug is enabled, this this should be visible on the page.</li>
<li>To <span class="highlight">hide the flash object</span> and disable the <span class="highlight">log messages appending to the bottom of the page</span>, set debug to false</span>. (Log messages are added to a div if no console is found).</li>
<li>If the user does not have flash installed, the onerror function will be called after a (configurable) 10 second timeout. You may want to use a library such as <a href="http://www.featureblend.com/javascript-flash-detection-library.html">Flash Detect</a> to check for this more quickly. <span  class="highlight">Flash Player 9.0.31.0</span> or newer is required.</li>
<li>If you pass a <span class="highlight">non-string data</span> as the key or value, things may break. Your best bet is to use strings and/or use <a href="http://json.org">JSON</a> to encode objects as strings.</li>
<li>If you see the error <span class="highlight">&#8220;uncaught exception: Error in Actionscript. Use a try/catch block to find error.&#8221;</span>, try using &#8220;//&#8221; in the .swf URL rather than &#8220;https://&#8221;. See <a href="https://github.com/nfriedly/Javascript-Flash-Cookies/issues/14">https://github.com/nfriedly/Javascript-Flash-Cookies/issues/14</a> for more information.</li>
<li>Do not set <code class="highlight">display:none</code> on the swf or any of it&#8217;s parent elements, this will cause the file to not render and the timeout will be fired. Disable debug and it will be rendered off screen.</li>
<li>The error <code class="highlight">this.swf.set is not a function</code> has been known to occur when the FlashFirebug plugin is enabled in Firefox / Firebug..
</ul>
<h3>Patches</h3>
<p>Although my JS is solid, my Flash / ActionScript skills leave something to be desired. Patches to either are more than welcome at <a href="http://github.com/nfriedly/Javascript-Flash-Cookies">github</a> (preferred), or just leave a comment here if you&#8217;re not sure how to use github. (This comment has a <a href="http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/comment-page-1/#comment-2531">short walk through to using github</a>.)</p>
<h3>Production Use</h3>
<p>If you&#8217;re using SwfStore in a production site, feel free to leave a comment here with a link to the site. I turned off WP&#8217;s default rel=&#8221;nofollow&#8221;, so enjoy the link juice <img src='http://nfriedly.com/techblog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Reciprocal links are not required, but are always appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://nfriedly.com/techblog/2010/07/swf-for-javascript-cross-domain-flash-cookies/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>

