1
Well recently I made a login page where you have the normal login and the by facebook, only when login by facebook when you click redirects to the application to allow access, ok so far so good, only when you click and allows it returns to the page with a code in the browser only it displays the error message Error connection with Facebook ID and Secret are right!
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['code'])){
// Informe o seu App ID abaixo
$appId = '680167435480727';
// Digite o App Secret do seu aplicativo abaixo:
$appSecret = 'xxxxxxx';
// Url informada no campo "Site URL"
$redirectUri = urlencode('$path');
// Obtém o código da query string
$code = $_GET['code'];
// Monta a url para obter o token de acesso e assim obter os dados do usuário
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $appId . "&redirect_uri=" . $redirectUri
. "&client_secret=" . $appSecret . "&code=" . $code;
//pega os dados
$response = @file_get_contents($token_url);
if($response){
$params = null;
parse_str($response, $params);
if(isset($params['access_token']) && $params['access_token']){
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
// nesse IF verificamos se veio os dados corretamente
if(isset($user->email) && $user->email){
/*
*Apartir daqui, você já tem acesso aos dados usuario, podendo armazená-los
*em sessão, cookie ou já pode inserir em seu banco de dados para efetuar
*autenticação.
*No meu caso, solicitei todos os dados abaixo e guardei em sessões.
*/
$_SESSION['email'] = $user->email;
$_SESSION['nome'] = $user->name;
$_SESSION['uid_facebook'] = $user->id;
$mail = $_SESSION['email'];
$name = $_SESSION['nome'];
$name = $_SESSION['uid_facebook'];
$check = mysql_num_rows(mysql_query("SELECT * FROM users WHERE mail = '$mail' LIMIT 1"));
if($check==0){
// REGISTRO
function GenerateName($mail){
$name = current(explode('@', $mail));
return $name;
}
$username = GenerateName($mail); // Nombre generado
$password = substr(md5(rand(0, 999). strtolower($mail). rand(0,100000)), 0, 12);
mysql_query("INSERT INTO `users` (username,real_name,password,auth_ticket,motto,mail,rank,look,gender,account_created,last_online,online,ip_last,ip_reg,working,secretcode,mymusik,home_room,vip) VALUES ('".mysql_real_escape_string($username)."','Habbo','".mysql_real_escape_string($password)."','-/-','".$sitename." <3','".mysql_real_escape_string($mail)."','1','".$look."','".$gender."','".time()."','".time()."','1','".$remote_ip."','".$remote_ip."','','','','0','1')") or die(mysql_error());
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header('location: '.$path.'/welcome');
exit();
}else{
// LOGIN
$userq1 = mysql_query("SELECT * FROM users WHERE mail = '$mail' LIMIT 1");
while($row = mysql_fetch_assoc($userq1)){
$credUser = $row['username'];
$credPass = $row['password'];
$_SESSION['username'] = $credUser;
$_SESSION['password'] = $credPass;
header('location: '.$path.'/me');
}
}
}
}else{
$login_fehler = "Ocorreu um erro de código. Contate-nos através de nosso facebook fb.com/HebbiBrasil";
exit(0);
}
}else{
$login_fehler = "Ocorreu um erro de código. Contate-nos através de nosso facebook fb.com/HebbiBrasil";
exit(0);
}
}else if($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['error'])){
$login_fehler = "Ocorreu um erro de código. Contate-nos através de nosso facebook fb.com/HebbiBrasil";
}
?>
I recommend that you remove App Secret from your question, since it is something secret (as the name says) and specific to your application. I suggest you re-define it as well since it was put here.
– KillerJack
Have you downloaded the Facebook library? Is it related to the current file? Take a look here. It is the most updated code currently on the Internet for login with the Face SDK. http://www.krizna.com/general/login-with-facebook-using-php/ Besides of course, be totally commented! Look there and tell me.
– WebCraft
Remove "@" to not omit errors, you want to know what is wrong, do not use "@" to omit it. If you are using
file_get_contents
you need to allow it to connect to external website in caseallow_url_fopen
in thephp.ini
– Inkeliz
You don’t need the SDK, everything can be done using the Curl or the
file_get_contents
/fopen
, PHP natives, including is also documented in https://developers.facebook.com/docs/graph-api/reference all documents endpoints to be used.– Inkeliz
Oh yes. It’s also used. But I figured that even though it used file_get_contents($token_url);, there was some related directory that had not been done the link (require)
– WebCraft