confused about cookies and redirecting

This is the first time ive tried to make a facebook login site.
Im trying to make a site cookie storing facebook profile information (without using facebook cookie).
Im abit confused about the flow of redirection… when i get to my register page,
it cant find the cookie im trying to create, it goes into the else (couldnt find cookie).
I was wondering if someone can point me to where im going wrong?



<?php require 'facebook-php-sdk/src/facebook.php'; $facebook = new Facebook(array( // real // /* 'appId' => '***', 'secret' => '***', */ // dev // 'appId' => '***', 'secret' => '***' , )); // See if there is a user from a cookie $user = $facebook->getUser(); // if user has session cookie, then is logged in and is redirected to homepage. if (isset($_COOKIE['mywebCookie'])) { // redirect to homepage header('HTTP/1.1 302 Found'); header('Location: http://localhost:8888/'); exit; } // if user doesnt have session cookie, but has facebook authentication. if ($user && !isset($_COOKIE['mywebCookie'])) { try { $user_profile = $facebook->api('/me'); // create a site session cookie from facebook profile. // import config/constants /* expire in 1 hour */ setcookie("mywebCookie", $user_profile, time()+3600, "/", "http://localhost:8888/"); // redirect to finish registration page header('HTTP/1.1 302 Found'); header('Location: http://localhost:8888/'); exit; } catch (FacebookApiException $e) { echo '
'.htmlspecialchars(print_r($e, true)).'
'; $user = null; } } ?>


<!DOCTYPE html>
<html xmlns:fb="">
    [php]<?php if(!$user) { ?>[/php]
<fb:login-button scope="email, user_birthday, user_location, user_subscriptions, user_website, user_education_history, user_work_history, user_events, user_interests, create_event, rsvp_event"></fb:login-button>
    [php]<?php } ?>[/php]
    <div id="fb-root"></div>
      window.fbAsyncInit = function() {
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: false,
          xfbml: true,
          oauth: true,
          status: true, // check login status
          channelUrl: 'http://localhost:8888/' //custom channel
        FB.Event.subscribe('auth.login', function(response) {
        //  window.location.reload();
          window.location = " http://localhost:8888/";
        //  	window.location = "http://localhost:8888/";
        FB.Event.subscribe('auth.logout', function(response) {
       //   window.location = "";
      //		window.location = "localhost:8888/";
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +



<?php echo ''; require_once ''; echo ''; echo '
'; require_once ''; if (isset($_COOKIE['mywebCookie'])) { require_once 'register.content.php'; } else{ echo ' // you are not logged in' .'
' .$_COOKIE["mywebCookie"]; }; require_once ''; echo '
'; ?>


I haven’t done much facebook integration stuff myself, but I’m curious if there is an issue with your “channel” being set to ‘http://localhost…’.

Does facebook not postback to this address (and won’t be able to resolve localhost). - Should this not be a public facing web address?

i tried commenting out the channel file ( apparently not always required). no luck.
the cookie is not being created, but i dont understand why.

PHP Notice:  Undefined index: mywebCookie in /Applications/MAMP/htdocs/ on line 19

is it something to do with when the javascript kicks in?
because i have both the javascript (when facebook authenticates) and php (headers) redirecting to register.php but im not sure if that is correct (cos im a n00b at php/facebook/javascript).
I have however tried commenting out the php header redirect and it didnt change, still cant find cookie.
I think if a ninja doesnt arrive over night, i will try a live version see if anything differs.

Sponsor our Newsletter | Privacy Policy | Terms of Service