2011-10-11 53 views
0

我在最近的一个应用程序中遇到了一些问题。 其投票应用程序应该允许每个用户每天投票一次。 所以我采取用户ID并将每个用户ID放在数据库中,但偶尔我得到用户ID = 0,我不知道为什么。我正在获取浏览器信息,并希望检查它是否与浏览器相关,但它发生在Mozilla和IE上。获取用户ID有时不工作?

这是下面的代码,如果有帮助。

的index.php(代码我使用nonfans)

<?php header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');?> 
<?php 
require 'facebook.php'; 
$app_id = "xxx"; 
$app_secret = "xxx"; 
// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $app_secret, 
    'cookie' => true 
)); 

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl(array('canvas' => 1, 
             'fbconnect' => 0, 
             'next' => 'xxx', 
             'cancel_url' => 'xxx')); 
    echo("<script> top.location.href='" . $loginUrl . "'</script>"); 
} 

$signed_request = $facebook->getSignedRequest(); 
function parsePageSignedRequest() { 
    if (isset($_REQUEST['signed_request'])) { 
     $encoded_sig = null; 
     $payload = null; 
     list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2); 
     $sig = base64_decode(strtr($encoded_sig, '-_', '+/')); 
     $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true)); 
     return $data; 
    } 
    return false; 
    } 
    if($signed_request = parsePageSignedRequest()) { 
    if($signed_request->page->liked) { 
     echo("<script> location.href='revealed.php'</script>"); 
    } else { 
     echo "<img src=\"non-fans.jpg\" width=\"520\" height=\"483\">"; 
    } 
    } 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 
<body style="text-align:center; margin:0; overflow:hidden;"> 
<div id="fb-root"></div> 
<script type="text/javascript"> 
    window.fbAsyncInit = function() { 
    FB.init({appId: 'xxx', status: true, cookie: false, xfbml: true}); 
    FB.Canvas.scrollTo(0,0); 
    window.setTimeout(function() { 
    FB.Canvas.setAutoResize(); 
    }, 250); 
    }; 
    (function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + 
     '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 
</body> 
</html> 

这里是为揭示标签的代码 - 主要的应用程序。

<?php 
require 'facebook.php'; 
$app_id = "xxx"; 
$app_secret = "xxx"; 
// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => $app_id, 
    'secret' => $app_secret, 
)); 

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

function getBrowser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown'; 
    $platform = 'Unknown'; 
    $version= ""; 

    //First get the platform? 
    if (preg_match('/linux/i', $u_agent)) { 
     $platform = 'linux'; 
    } 
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) { 
     $platform = 'mac'; 
    } 
    elseif (preg_match('/windows|win32/i', $u_agent)) { 
     $platform = 'windows'; 
    } 

    // Next get the name of the useragent yes seperately and for good reason 
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
     $bname = 'Internet Explorer'; 
     $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
     $bname = 'Mozilla Firefox'; 
     $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
     $bname = 'Google Chrome'; 
     $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
     $bname = 'Apple Safari'; 
     $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
     $bname = 'Opera'; 
     $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
     $bname = 'Netscape'; 
     $ub = "Netscape"; 
    } 

    // finally get the correct version number 
    $known = array('Version', $ub, 'other'); 
    $pattern = '#(?<browser>' . join('|', $known) . 
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; 
    if (!preg_match_all($pattern, $u_agent, $matches)) { 
     // we have no matching number just continue 
    } 

    // see how many we have 
    $i = count($matches['browser']); 
    if ($i != 1) { 
     //we will have two since we are not using 'other' argument yet 
     //see if version is before or after the name 
     if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){ 
      $version= $matches['version'][0]; 
     } 
     else { 
      $version= $matches['version'][1]; 
     } 
    } 
    else { 
     $version= $matches['version'][0]; 
    } 

    // check if we have a number 
    if ($version==null || $version=="") {$version="?";} 

    return array(
     'userAgent' => $u_agent, 
     'name'  => $bname, 
     'version' => $version, 
     'platform' => $platform, 
     'pattern' => $pattern 
    ); 
} 

// now try it 
$ua=getBrowser(); 
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent']; 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" type="text/css" href="http://meyerweb.com/eric/tools/css/reset/reset.css" media="all" /> 
<link rel="stylesheet" type="text/css" href="style.css" media="all" /> 
<script src="jquery-1.6.4.min.js" type="text/javascript"></script> 
<script src="jquery.validate.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#voting-form").validate(); 
}); 
</script> 
</head> 
<body> 
<div id="fb-root"></div> 
<script type="text/javascript"> 
    window.fbAsyncInit = function() { 
    FB.init({appId: 'xxx', status: true, cookie: false, xfbml: true}); 
    FB.Canvas.scrollTo(0,0); 
    window.setTimeout(function() { 
    FB.Canvas.setAutoResize(); 
    }, 250); 
    }; 
    (function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + 
     '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 

让我知道如果你需要查询信息,但我doubg多数民众赞成这个问题,因为它工作在99个的100票

回答

2

一些交流中心的原因,有时服务器无法识别该用户已经登录(至少这是我认为,因为我有同样的问题),所以你必须尝试再次登录用户以获得正确的访问令牌。 这是我要做的事:

$user = $facebook->getUser(); 
if(!$user) 
{ 
    $loginUrl = $facebook->getLoginUrl(array('scope'=>'publish_stream,email')); 
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; 
    exit; 
} 

而且它应该工作,但是我真的不能保证你任何东西。希望这可以帮助。