我是PHP的新手,我对以下问题感到困惑。当facebook用户登录时,该用户被添加到数据库中。但是,当同一用户再次登录时,应该打印出“用户名已经存在”'而是它再次将同一个用户插入数据库。我如何解决它?任何帮助表示赞赏
<?php
echo "<p> Hello World!</p>";
// php 5.3 and up can throw an error if this is not set
date_default_timezone_set("Europe/London");
// much of the example code on the web forgets to include these HttpClients, for some reason
require_once( '../docs/Facebook/HttpClients/FacebookHttpable.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurl.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( '../docs/Facebook/FacebookSession.php' );
require_once( '../docs/Facebook/FacebookRedirectLoginHelper.php' );
require_once( '../docs/Facebook/FacebookRequest.php' );
require_once( '../docs/Facebook/FacebookResponse.php' );
require_once( '../docs/Facebook/FacebookSDKException.php' );
require_once( '../docs/Facebook/FacebookRequestException.php' );
require_once( '../docs/Facebook/FacebookAuthorizationException.php' );
require_once( '../docs/Facebook/GraphObject.php' );
// This one is also often left out
require_once( '../docs/Facebook/Entities/AccessToken.php');
// store your $HOSTNAME, $APPID and $SECRET in this file:
require_once( '../docs/my_app_credentials.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
session_start();
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($APPID,$SECRET);
// login helper with uri
$PAGENAME="page2.php";
$REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME;
$helper = new FacebookRedirectLoginHelper( $REDIRECT_URL );
try {
echo "<p> about to try to get session: the helper variable: </p>";
$session = $helper->getSessionFromRedirect();
// echo "<p> the session variable:</p>";
// var_dump($session);
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
echo "<p> There was a facebook request exception</p>";
} catch( \Exception $ex ) {
echo "<p> There was a validation failure</p>";
var_dump($ex);
// When validation fails or other local issues
}
echo "<p> 'get session' block now completed...</p>";
// see if we have a session
if ( isset( $session ) ) {
echo "<p> Now try to get a graph node</p>";
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
$FBID=$graphObject->getProperty('id');
$FIRSTNAME=$graphObject->getProperty('first_name');
$SECONDNAME=$graphObject->getProperty('last_name');
$TOKEN=$_GET['code'];
//write code to check if current user is in database
//if not add them to database
//finally, give link to page3.php which removes them (user) from database.
$con = mysqli_connect("localhost", "", "", "");
$query = "SELECT FBID FROM USERS WHERE FBID='$FBID'";
$result = $con->query($query);
if (!$result)
{
echo "Username already exists";
}
else
{
$con->query("INSERT INTO USERS (FBID,FIRSTNAME,SECONDNAME,TOKEN) VALUES ('$FBID','$FIRSTNAME','$SECONDNAME','$TOKEN')");
echo"user inserted";
}
} else {
// show login url
echo 'No session was set. Try logging in again by clicking here: <a href="' . "page1.php". '">Login</a>';
}
?>
答案 0 :(得分:0)
您正在检查!$query
,但它会一直返回一些内容,因此它会转到else
。
您可以做的是检查返回的行数:
if ($result->numRows() > 0) {
//if more than 0 rows returned (e.g, one row)
echo "Username exists!";
} else {
//insert to db
}
或者,更简单,做if ($result)
,只需摆脱!
!