Note that there are some explanatory texts on larger screens.

plurals
  1. POfacebook is redirecting to my site outside of facebook
    primarykey
    data
    text
    <p>I'm having trouble working with my redirect uri in the authentication.</p> <p>If i set it to my site, the user will authenticate, because <code>$_Request['code']</code> is set, but then the user will be on my site, and I don't want that</p> <p>If I redirect to apps.facebook.com/myapp, then <code>$_Request['code']</code> is not set, and the user won't authenticate, but just see a blank page.</p> <p>is there any way to do this in PHP, I have code running before the page is rendered.</p> <p>How do you guys solve this issue?</p> <p>my login function:</p> <pre><code>public static function login($redirect) { $app_id = AppInfo::appID(); $app_secret = AppInfo::appSecret(); $home = urlencode(AppInfo::getHome()); // See https://developers.facebook.com/docs/reference/api/permissions/ // for a full list of permissions $scope = 'user_photos,publish_stream'; session_start(); $code = $_REQUEST["code"]; // If we don't have a code returned from Facebook, the first step is to get if (empty($code)) { // CSRF protection - for more information, look at 'Security Considerations' // at 'https://developers.facebook.com/docs/authentication/' $state = md5(uniqid(rand(), TRUE)); setcookie( AppInfo::appID() . '-fb-app', $state, $expires = 0, $path = "", $domain = "", $secure = "", $httponly = true); // Now form the login URL that you will use to authorize your app $authorize_url = "https://www.facebook.com/dialog/oauth?client_id=$app_id" . "&amp;redirect_uri=$home&amp;state=" . $state . "&amp;scope=$scope"; // Now we redirect the user to the login page echo("&lt;script&gt; window.location.href='" . $authorize_url . "'&lt;/script&gt;"); return false; // Once we have that code, we can now request an access-token. We check to // ensure that the state has remained the same. } else if ($_REQUEST['state'] === $_COOKIE[AppInfo::appID() . '-fb-app']) { $ch = curl_init("https://graph.facebook.com/oauth/access_token"); curl_setopt($ch, CURLOPT_POSTFIELDS, "client_id=$app_id&amp;redirect_uri=$home&amp;client_secret=$app_secret" . "&amp;code=$code&amp;scope=$scope"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); // Once we get a response, we then parse it to extract the access token parse_str($response, $params); $token = $params['access_token']; return $token; // In the event that the two states do not match, we return false to signify // that something has gone wrong during authentication } else { echo("States do not match. CSRF?"); return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload