PHP-Facebook登录
PHP-Facebook登录 我们可以使用Facebook登录名来允许用户访问网站。此页面将向您说明有关使用F…
PHP-Facebook登录
我们可以使用Facebook登录名来允许用户访问网站。此页面将向您说明有关使用Facebook PHP SDK登录的信息。
用Facebook登录
-
需要转到https://developers.facebook.com/apps/并单击添加新组按钮以创建应用程序ID。
-
选择网站
-
输入应用名称,然后单击“创建新的Facebook应用ID”
-
点击创建应用程序ID
-
单击跳过快速测试
在最终阶段,它将显示如下图所示。
fbconfig.php文件概述
-
现在打开fbconfig.php文件并添加您的应用程序ID和应用程序Secrete
FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' ); // login helper with redirect_uri $helper = new FacebookRedirectLoginHelper('You web address' );
最后是fbconfig.php文件,如下所示-
<?php session_start(); // added in v4.0.0 require_once 'autoload.php'; use FacebookFacebookSession; use FacebookFacebookRedirectLoginHelper; use FacebookFacebookRequest; use FacebookFacebookResponse; use FacebookFacebookSDKException; use FacebookFacebookRequestException; use FacebookFacebookAuthorizationException; use FacebookGraphObject; use FacebookEntitiesAccessToken; use FacebookHttpClientsFacebookCurlHttpClient; use FacebookHttpClientsFacebookHttpable; // init app with app id and secret FacebookSession::setDefaultApplication( '496544657159182','e6d239655aeb3e496e52fabeaf1b1f93' ); // login helper with redirect_uri $helper = new FacebookRedirectLoginHelper('http://www.tutorialspoint.com/' ); try { $session = $helper-?>getSessionFromRedirect(); }catch( FacebookRequestException $ex ) { // When Facebook returns an error }catch( Exception $ex ) { // When validation fails or other local issues } // see if we have a session if ( isset( $session ) ) { // graph api request for user data $request = new FacebookRequest( $session, 'GET', '/me' ); $response = $request->execute(); // get response $graphObject = $response->getGraphObject(); $fbid = $graphObject->getProperty('id'); // To Get Facebook ID $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name $femail = $graphObject->getProperty('email'); // To Get Facebook email ID /* ---- Session Variables -----*/ $_SESSION['FBID'] = $fbid; $_SESSION['FULLNAME'] = $fbfullname; $_SESSION['EMAIL'] = $femail; /* ---- header location after session ----*/ header("Location: index.php"); }else { $loginUrl = $helper->getLoginUrl(); header("Location: ".$loginUrl); } ?>
登录页面概述
登录页面用于登录FB
<?php session_start(); session_unset(); $_SESSION['FBID'] = NULL; $_SESSION['FULLNAME'] = NULL; $_SESSION['EMAIL'] = NULL; header("Location: index.php"); ??>
Index.php
索引页面如下所示。
<?php session_start(); ??> <title>Login with Facebook</title> <link href="http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet"> <?php if ($_SESSION['FBID']): ??> <!-- After user login --> <div> <div> <h2>Hello <?php echo $_SESSION['USERNAME']; ??></h2> <p>Welcome to "facebook login" tutorial</p> </div> <div> <ul> <li>Image</li> <li><img src="https://mangdo-1254073825.cos.ap-chengdu.myqcloud.com//front_eng_imgs/programiz/7f9013248e9a8fc92b5f7713c29c68ef722afa2b403e6538c4980998_23.jpg"></li> <li>Facebook ID</li> <li><?php echo $_SESSION['FBID']; ??></li> <li>Facebook fullname</li> <li><?php echo $_SESSION['FULLNAME']; ??></li> <li>Facebook Email</li> <li><?php echo $_SESSION['EMAIL']; ??></li> <div><a href="logout.php">Logout</a></div> </ul> </div> </div> <?php else: ??> <!-- Before login --> <div> <h2>Login with Facebook</h2> Not Connected <div> <a href="fbconfig.php">Login with Facebook</a> </div> <div> <a href="http://www.tutorialspoint.com" title="Login with facebook">More information about Tutorialspoint</a> </div> </div> <?php endif ??>
它将产生结果。在尝试此示例之前,请在浏览器中注销脸书帐户。
登出Facebook
下面的代码用于注销Facebook。
<?php session_start();. session_unset(); $_SESSION['FBID'] = NULL; $_SESSION['FULLNAME'] = NULL; $_SESSION['EMAIL'] = NULL; header("Location: index.php"); ??>
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!