I have made a facebook application which when opened from apps.facebook.com/myapp
points perfectly to my domain's index.php and can be seen in the facebook app window , but problem occurs when i try to redirect it to my register page ,it gets redirected to my domain , i want all the pages to be viewed in facebook app window . tried to use fb:redirect sdk function but site says they are going to deprecate these codes.
<?php
if(preg_match('/apps.facebook.com/',$_SERVER[HTTP_REFERER])){
$app_id = '';
$api_key = '';
$app_secret = '';
$canvas_page = 'mydomain/index.php';
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=".
$app_id."&redirect_uri=".urlencode($canvas_page)."&scope=email,user_birthday,user_interests,user_about_me";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload,'-_', '+/')), true);
if (empty($data["user_id"])){
echo("<script> top.location.href='".$auth_url ."'</script>");
}else{
$canvas_page="mydomain/register.php";
echo ("<script> top.location.href='".$auth_url."'</script>");
}
}else{
echo "No facebook";
}
?>
the code works perfectly up till echo("<script> top.location.href='" .$auth_url ."'</script>")
when $canvas_page
is the one set in facebook app dev form ; but gets redirected 开发者_运维问答to some other page when $canvas_page
is changed to my register.php page . where am i going wrong ?
thanks in advance
<?php
//facebook application
//set facebook application id, secret key and api key here
$fbconfig['appid' ] = "123456392899383";
$fbconfig['api' ] = "97eb2asdfasdf3f20d4421b0fe8c1b2";
$fbconfig['secret'] = "5c1d4asdfasdf71b59806b69c386b2ca";
//set application urls here
$fbconfig['baseUrl'] = "http://www.your-url.com/";
$fbconfig['appBaseUrl'] = "http://apps.facebook.com/appname/";
$uid = null; //facebook user id
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
//PUT THE APPLICATION CODE HERE>
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
You can use this script to properly authorise the app an regular use it.
your $auth_url doesn't change:
$canvas_page="mydomain/register.php";
echo ("<script> top.location.href='".$auth_url."'</script>");
$auth url is still the url defined at the top with $canvas_page = 'mydomain/index.php';
This has nothing to do with facebook
. It's ABC PHP!!!
<?php
$a = "John";
$b = "Hello " . $a;
$a = "Bob";
print $b; // returns: Hello John
$b = "\nHello " . $a;
print $b; // returns: Hello Bob
?>
Live example.
精彩评论