Facebook Connect Intermittently Failing

With the system I’m using, the site’s user database is synced with Facebook Connect. Intermittently, ‘0’ is being returned as the Facebook user id.
Any ideas what would cause this? The code is below.
Thank you E.

define('FACEBOOK_APP_ID', '[api code]');
define('FACEBOOK_SECRET', '[secret]');

function parse_signed_request($signed_request, $secret) {
	list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

	// decode the data
	$sig = base64_url_decode($encoded_sig);
	$data = json_decode(base64_url_decode($payload), true);

	if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
		error_log('Unknown algorithm. Expected HMAC-SHA256');
		return null;

	// check sig
	$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
	if ($sig !== $expected_sig) {
		error_log('Bad Signed JSON signature!');
		return null;

	return $data;

function base64_url_decode($input) {
	return base64_decode(strtr($input, '-_', '+/'));

$response = parse_signed_request($_REQUEST['signed_request'], 
$user_id = $response['user_id'];
$first_name = $response['registration']['first_name'];
$last_name = $response['registration']['last_name'];
$email = $response['registration']['email'];
$pass = $response['registration']['password'];
$volunteer = $response['registration']['volunteer'];
$sharing = $response['registration']['sharing'];
$teacher = $response['registration']['teacher'];

if(empty($volunteer)) {$volunteer = 0;}
if(empty($sharing)) {$sharing = 0;}
if(empty($teacher)) {$teacher = 0;}

$key = PKEY; // string. Please make it a good one and store securely
$encryptor = new pcrypt($key); // init class
$query="SELECT * FROM volunteers WHERE username='$email'";
$result=mysql_query($query) or die(mysql_error()." line:".__LINE__);

if ($u>0){ 

	header('location: /error');

if($merge) { //Merge with existing account
	$query="UPDATE volunteers SET validated='$validated', first_name='$first_name', last_name='$last_name', email='$email', sharing='$sharing', volunteer='$volunteer', teacher='$teacher', password='$password', facebook_id='$user_id' WHERE email='$email'";
	$next_page = "Location: http://" . $_SERVER['HTTP_HOST'] . "/volunteerregistration?fb_merge_success=true";
} else { //insert new record	
	INSERT INTO volunteers 
	(id, validated, first_name, last_name, email, sharing, volunteer, teacher, username, password, facebook_id) 
	VALUES ( '','$validated', '$first_name', '$last_name', '$email', '$sharing', '$volunteer', '$teacher', '$email', '$password', '$user_id')
	$next_page = "Location: /volunteer_portal.php";

I figured out the problem.
It was the Facebook like button interfering with Facebook Connect.
I fixed it by using the iFrame implementation.