未定义索引错误,$code = $_REQUEST[“code”];用于 Facebook 身份验证

发布于 2024-12-06 05:09:10 字数 1164 浏览 0 评论 0原文

每次我进入 Facebook 应用程序的画布页面时,都会短暂显示一条错误,指出 $code = $_REQUEST["code"]; 行上未定义索引;在第 9 行。此代码显示后,看起来它执行了快速重定向,并且一切正常。有谁知道如何解决这个问题?这是我在画布顶部的代码:

<?php 

$app_id = "244958008880978";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "https://apps.facebook.com/guessyguesser/";
session_register();
session_start();

$code = $_REQUEST["code"];

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



{
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_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(file_get_contents($graph_url));
}

?>

谢谢!

Every time I go to the canvas page of my facebook app it briefly shows an error saying undefined index on the line with $code = $_REQUEST["code"]; at line 9. After this code shows it looks like it does a quick redirect and everything works fine. Does anyone know how to solve this? Here's my code at the top of the canvas:

<?php 

$app_id = "244958008880978";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "https://apps.facebook.com/guessyguesser/";
session_register();
session_start();

$code = $_REQUEST["code"];

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



{
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_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(file_get_contents($graph_url));
}

?>

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相权↑美人 2024-12-13 05:09:11

可以在第 9 行的开头添加一个 @,或者将第 9 行移到 if 块下方,并将第 11 行更改为

if (!isset($_REQUEST["code"])) {

Oh,并在 exit 后面添加一个 exit 语句。代码>回显。

Either add an @ to the beginning of line 9, or move line 9 below the if block and change line 11 to

if (!isset($_REQUEST["code"])) {

Oh, and also add an exit statement right after that echo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文