2012-03-01 180 views
1

我正在使用Facebook PHP SDK 3.1.1 此頁面應該從頭文件收集值,稍後將其用於上傳圖像。Facebook應用程序重定向後會丟失會話值

問題:$_SESSION['file']在用戶登錄Facebook後變成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']是空

我這樣做對嗎?或者我的虛擬主機有問題?

回答