使用 PHP SDK 进行身份验证

发布于 2024-12-29 06:22:54 字数 1063 浏览 0 评论 0原文

我在使用 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 技术交流群。

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

发布评论

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

评论(3

圈圈圆圆圈圈 2025-01-05 06:22:54

试试这个

<?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'
);

$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params); 

<?php if ($user) { ?>
<p><a href="<?=$logoutUrl?>">Logout</a></p>
<?php } else { ?>
  <p><a href="<?=$loginUrl?>">Click here to View Your Stalker of the Day</a></p>
<?php } ?>

Try this out

<?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'
);

$loginUrl = $facebook->getLoginUrl($fbparams);
$params = array( 'next' => 'xxx' );
$logoutUrl = $facebook->getLogoutUrl($params); 

<?php if ($user) { ?>
<p><a href="<?=$logoutUrl?>">Logout</a></p>
<?php } else { ?>
  <p><a href="<?=$loginUrl?>">Click here to View Your Stalker of the Day</a></p>
<?php } ?>
零時差 2025-01-05 06:22:54

在创建实例之前您不能调用 $facebook,此代码应该可以工作:

<?php
session_start();
require_once("facebook.php");

$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';

$facebook = new Facebook($config);
$user = $facebook->getUser();


$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);

$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";


        }

?>

You can not call $facebook before creating the instance, This code should work:

<?php
session_start();
require_once("facebook.php");

$config = array();
$config[‘appId’] = xxx;
$config[‘secret’] = '{secret}';

$facebook = new Facebook($config);
$user = $facebook->getUser();


$fbparams = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'xxx'
);

$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";


        }

?>
枕花眠 2025-01-05 06:22:54
$params = array( 'next' => 'xxx' );

已弃用 SDK,请

$params = array( 'redirect_uri' => 'xxx' );

改用。

$params = array( 'next' => 'xxx' );

is deprecated SDK, use

$params = array( 'redirect_uri' => 'xxx' );

instead.

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