I have a problem to store my Facebook data (login data) into my personal SQL database.
Here You can find my code: I use first an index page, then a login page to store my facebook data login in my database. In my login page I redirect to my user_page. But when I redirect to my user_page the Facebook data is not stored and I get the link https://localhost/project/login.php?error_code=901&error_message=This+app+is+in+sandbox+mode.++Edit+the+app+configuration+at+http%3A%2F%2Fdevelopers.facebook.com%2Fapps+to+make+the+app+publicly+visible.&state=7504294781f7ae045299820cb5c40ead#_=_
Index page code
<?php
require 'facebook-php-sdk/src/facebook.php';
define('APP_URL', 'https://localhost/project/login.php');
define('APP_PERMISSIONS', '');
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'SECRET',
'cookie' => true
));
$loginUrlParams = array(
'scope' => APP_PERMISSIONS,
'redirect_uri' => APP_URL,
);
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fbuid = $facebook->getUser();
$user_profile = $facebook->api('/me');
// header('Location: user_page.php');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
header('Location: ' . $facebook->getLoginUrl($loginUrlParams));
exit;
}
}
else
{
header('Location: ' . $facebook->getLoginUrl($loginUrlParams));
exit;
}
?>
Login page code:
<?php
define("db_DATABASE", "phples");
define("db_SERVER", "localhost");
define("db_USER", "root");
define("db_PASS", "");
$conn=new mysqli(db_SERVER,db_USER,db_PASS,db_DATABASE);
require 'facebook-php-sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'SECRET',
'cookie' => true));
// Get User ID
$user = $facebook->getUser();
if($user){
$user_profile = $facebook->api('/me');
$query = mysql_query("SELECT * FROM facebook_users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO facebook_users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");
header('Location: user_page.php');
}else{
header('Location: user_page.php');
}
}else{
header('Location: logout.php');
}
?>
User_page code:
<?
require 'facebook-php-sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'SECRET',
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fbuild = $facebook->getUser();
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$paramsout = array('next'=>'http://localhost/project/logout.php');
$logoutUrl = $facebook->getLogoutUrl($paramsout);
}
?>
The error says what might be the issue with your application. As it says
error_message=This+app+is+in+sandbox+mode.++Edit+the+app+configuration+at+http%3A%2F%2Fdevelopers.facebook.com%2Fapps+to+make+the+app+publicly+visible.
You are running your application in sandbox mode. In this mode only the developer, tester and other people associated with the App can work with it but not any one other than them. So change your app settings and disable the sandbox mode
.
Edit
As per your comment the error states
One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains
And as it says, you should add https://localhost/
in your Website with Facebook Login
within your app settings so that Facebook may redirect back to your page.