使用 PHP SDK 进行身份验证
我在使用 PHP SDK 进行身份验证时遇到问题。我想要达到的效果是,当用户访问该网站时,如果他们使用 FB 登录,他们会看到“注销”,点击后会将他们注销,但如果他们到达时未登录,他们应该看到“您需要登录” in with FB”,这会登录他们。我当前得到的效果是,即使用户已经登录,该网站也会显示“您需要使用 FB 登录”,当单击此按钮时,用户将被带到 facebook。 com 出现错误消息显示“发生错误。请稍后再试”。我确信我的代码中一定缺少一些东西,但不知道是什么,我对 FB 开发相当陌生。请参阅下面我的代码。任何帮助都非常感谢。
<?php
require_once("facebook.php");
$user = $facebook->getUser();
$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';
$facebook = new Facebook($config);
$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
session_start();
$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
if(!$user)
{
echo "<P>You need to <a href=\"{$loginUrl}\">log into FB</a></p>\n";
}
else
{
echo "<p style=\"margin-bottom:20px;\"><a href=\"{$logoutUrl} \">Logout</p>\n";
}
?>
I am having trouble using the PHP SDK for authentication. The effect I am trying to get is whene a user visits the site if they are loged in with FB they see "Logout" which loggs them out when clicked but if they are not logged in when they arrive they should see "You need to log in with FB" which loggs them in. The effect I am currently getting is that the site displays the "You need to log in with FB" even if the user is already logged in, whene this is clicked the user is taken to facebook.com with an error message displayed reading "An error occurred. Please try later". Im sure I must be missing something in my code but cant figure out what, I am fairly new to FB development. Please see my code below. Any help much appriciated.
<?php
require_once("facebook.php");
$user = $facebook->getUser();
$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';
$facebook = new Facebook($config);
$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);
session_start();
$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
if(!$user)
{
echo "<P>You need to <a href=\"{$loginUrl}\">log into FB</a></p>\n";
}
else
{
echo "<p style=\"margin-bottom:20px;\"><a href=\"{$logoutUrl} \">Logout</p>\n";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this out
在创建实例之前您不能调用 $facebook,此代码应该可以工作:
You can not call $facebook before creating the instance, This code should work:
已弃用 SDK,请
改用。
is deprecated SDK, use
instead.