facebook app重定向后丢失会话值

时间:2012-03-01 08:22:41

标签: session facebook-graph-api facebook-php-sdk

我正在使用Facebook PHP SDK 3.1.1 该页面应该从标题中收集值,然后用它来上传图像。

问题:用户登录Facebook后,$_SESSION['file']变为null

<?php
require 'facebook.php';
include('connect.php');
$facebook = new Facebook(array('appId'  => "XXXXXX",'secret' => "XXXXX","cookie" => true,'fileUpload' => true));
session_start();
$user_id = $facebook->getUser();
if(isset($_REQUEST["src"]))  
{
    echo "<center><img src='loader.gif'/></center>";
    echo "<center><div id='msg'>Exporting Image..</div></center>";
    $_SESSION['file'] = $_REQUEST["src"];
    $_SESSION['club'] = $_REQUEST["club"];

}
$code = $_REQUEST["code"];
if(empty($code)) 
{
    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); 
    $dialog_url = "http://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>");   

    exit();
}

$token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url). "&client_secret=" . $app_secret . "&scope=user_photos,email,read_stream,publish_stream&code=".$code;

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$response = url_get_contents($token_url);

$params = null;
parse_str($response, $params);

$graph_url = "https://graph.facebook.com/me?access_token=". $params['access_token'];

$user = json_decode(url_get_contents('https://graph.facebook.com/me/albums?access_token='.$params['access_token']),true);


foreach($user as $key=>$value)
{
    foreach($user[$key] as $key1=>$value1)
    {
        if($value1['name'] == "Photos")
        {
            $album_uid = $value1['id']; 
        }
    }
}

if(!isset($album_uid))
{
    echo 'Create an album';
    $album_details = array(
        'message'=> 'Uploaded via XXX',
        'name'=> 'Photos'
    );
    $create_album = $facebook->api('/me/albums', 'post', $album_details);
    $album_uid = $create_album['id'];
}
$photo_details = array('message'=>$_SESSION["club"]);

try{
    $file = NULL;
    $file="../Photos/".$_SESSION['file'].".jpeg";
    echo '<script>document.getElementById("msg").innerHTML = "Uploading Image"</script>';
    $photo_details['image'] = '@' . realpath($file);
    $upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
}catch(Exception $e){
    echo "<script type='text/javascript'>top.location.href = 'http://www.facebook.com/PAGE_URL/';</script>";
    exit();
}

$_SESSION['file']为空

我做得对吗?或者我的网站托管出了什么问题?

1 个答案:

答案 0 :(得分:1)

问题是您的应用程序在IFRAME内运行,并且会话cookie未被保留。请看这里:

Facebook Iframe App with multiple pages in Safari Session Variables not persisting