Facebook Connect problem

I am using some code to check if the user is logged in with facebook, but something strange happens, sometimes the code recognized the FB cookie and sometime it doesnt (i.e. if I refresh the page sometimes the cookie is recognized and then sometimes it asks me to login again - however cookie exists all times)

MY CODE:

 if(!isset($_COOKIE['fbsr_530028260370435'])) 
		 {
		 echo '<div style="padding-top:17px;"><div class="fb-login-button" style="float:left;" onlogin="javascript:CallAfterLogin();" size="medium" scope="publish_stream,email">Connect With Facebook</div><div class="clear"></div></div>';
		 }else { echo '<div class="upload_t"><a href="'.base_url().'index.php/main/myaccount" id="inline" class="btn btn btn-info">my uploads</a></div>';}?>     

I also checked in Firebug and keep getting this error:

FB.login() called when user is already connected.

Is there a known solution for checking the FB cookie server side?? Am I checking the correct cookie?

Try using Facebook PHP SDK, it will make things easier.