Categories


Safari Facebook Application Cookie

Ran into a random issue with the latest version of Safari recently, and wanted to post the solution here (since it took quite a bit of searching to figure out what was going on). ¬†Apparently Apple has changed Safari to not allow third party cookies through iFrames, meaning that Facebook applications break because your app won’t get a user session.

To fix this, you have to do a bit of redirect magic, at the very top of your application (or somewhere before output happens), you’ll need to do something like this:

<?php
$facebookpageurl = "http://www.facebook.com/YOURPAGEURL";
$facebookappid = "YOURFACEBOOKAPPID";

$browser = getBrowserName();
if($browser == "Apple Safari") {
	session_start();
	$page_url = $facebookpageurl . "/app_" . $facebookappid;
	
	if(isset($_GET['start_session']))
		die(header("Location:".$page_url));
	if(!isset($_GET['sid']))
		die(header("Location:?sid=".session_id()));
	$sid = session_id();
	if(empty($sid) || $_GET['sid'] != $sid):
	?>
		<script>
			top.window.location="?start_session=true";
		</script>
	<?php
	endif;
}

function getBrowserName() {
	$u_agent = $_SERVER['HTTP_USER_AGENT']; 
	if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) { 
		$bname = 'Internet Explorer'; 
	} elseif(preg_match('/Firefox/i',$u_agent)) { 
		$bname = 'Mozilla Firefox'; 
	} elseif(preg_match('/Chrome/i',$u_agent)) { 
		$bname = 'Google Chrome'; 
	} elseif(preg_match('/Safari/i',$u_agent)) { 
		$bname = 'Apple Safari'; 
	} elseif(preg_match('/Opera/i',$u_agent)) { 
		$bname = 'Opera'; 
	} elseif(preg_match('/Netscape/i',$u_agent)) { 
		$bname = 'Netscape'; 
	}
	return $bname;
}
?>


Your Comment