Good morning, I finally managed to store the Facebook usernames in my database after Facebook login. The only problem is that the page where the user is redirected to after Facebook login needs to be reloaded (I get just an empty page the first time). Can you please refer to my previous question as I've already posted the all code there
UPDATE: I noticed that the page needs to be refreshed only the first time (when the user info are not yet stored in the database, then it's loads the page quick and nicely. PLEASE HELP!;-)
UPDATE2: is there a way to auto refresh the page (just once) after the new user has been added? many Thanks!
UPDATE 3: I post the code.... it works only when i refresh the page.... any idea?
<?php
mysql_connect('host', 'username', 'password');
mysql_select_db('table');
require 'library/facebook.php';
// Create our Application instance
$facebook = new Facebook(array(
'appId' => 'MYID',
'secret' => 'MYSECRET',
'cookie' => true));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}else{
header('location: index.php');
}
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");
$query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
if ($user) {
$paramsout = array('next'=>'http://www.mywebsite/test/logout.php');
$logoutUrl = $facebook->getLogoutUrl($paramsout);
}
?>
UPDATE 4*****: I found a solution. You can look on my answer below. Thanks to everybody.
when facebook redirects using next or redirect URL it send the post data which the api gets but when you manually redirects your page it don't gets the facebook post data hence the code in else condition header('location: index.php');
is executed and you are redirected properly..
the problem for the first time is because it by passes the else condition and go to the sql to insert new record and after that their is no redirection.. so you need to put header('location: index.php');
in the end to redirect it after user record insertion in the db
hope this helps
Use
header("Location: url");
exit();
after all the necessary actions, assuming that you don't send any headers to this moment. Otherwise you may use META REFRESH or JS document.location.href in the resulting document.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With