Friday, 15 June 2012

Facebook PHP login decline end up in Facebook instead of my page -



Facebook PHP login decline end up in Facebook instead of my page -

i trying implement simple facebook login (connect) own web page.

what want see is: (1) fb dialog (2) if user accepts, getting 'yyyyy' (3) if user declines, getting 'zzzzz'.

what see instead is: (1) fb dialog go app/cancel (2) if user accepts, getting 'yyyyy' (3) if user declines, fb dialog go app/leave app (4) if user declines again, getting fb.

here code (i hid actual 3 values in beginning):

<?php session_start(); $app_id = 'xxxxxxxxxxxxxxx'; $app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $my_url = 'xxxxxxxxxxxxxxxxxxxxxxxxx'; require_once("facebook.php"); $config = array(); $config['appid'] = $app_id; $config['secret'] = $app_secret; $config['fileupload'] = false; // optional $facebook = new facebook($config); $ok_to_continue = false; $code = $_request["code"]; if(empty($code)) { $_session['state'] = md5(uniqid(rand(), true)); // csrf protection $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_session['state']; echo("<script> top.location.href='" . $dialog_url . "'</script>"); } else { // $code not empty if($_session['state'] && ($_session['state'] === $_request['state'])) { $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code; $response = file_get_contents_curl($token_url); $params = null; parse_str($response, $params); if (isset($params['access_token'])) { $_session['access_token'] = $params['access_token']; $ok_to_continue = true; } else { oops('error 2', ''); echo ('<p>==='.$response.'===</p>'); } } else { oops('error 1', ''); } } // user data: if ($ok_to_continue) { $user_id = $facebook->getuser(); if ($user_id) { seek { $user_profile = $facebook->api('/me'); } grab (facebookapiexception $e) { oops('error 3', $e); } } } function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, true); $data = curl_exec($ch); curl_close($ch); homecoming $data; } function oops($errmsg, $logmsg) { global $ok_to_continue; $ok_to_continue = false; if ($errmsg !== '') { echo('<p><b>' . $errmsg . '</b></p>'); } if ($logmsg !== '') { error_log($logmsg); } } ?> <?php if ($ok_to_continue): ?> yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy <?php else: ?> zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz <?php endif; ?>

php facebook login

No comments:

Post a Comment