Cross-site request forgery validation failed. Required param “state” missing - php

I have uploaded this code for solving facebook login problem. there're 3 files to implement this.. (login.php/playground_fb.php/member_index_fb.php) ... First you can find the simple button for href to playground_fb.php. and then there's some algorithm in playground_fb.php and then finally if user accept this app, then go to the member_index_fb.php with info sessions such as email address, first name, last name... I have searched a lot about these problems, but I only found the advice like input the session_start or stop the sessions or keep in 'www' like this... But it still remain unchanged... Who else help me please?
<!-- login.php -->
<div onclick="facebookLogin()" style="cursor: pointer;" align="center">
<img src="../build/img/login/login_facebook.png">
</div>
function facebookLogin() {
top.location.href = 'http://www.peeknchews.com/playground_fb.php';
}
<!-- playground_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '257184091349723',
'app_secret' => '6c079349483a1bc959ce515df4e8138a',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
if(isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
}
else {
$accessToken = $helper->getAccessToken();
}
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
//When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
//When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if(!isset($accessToken)) {
$permissions = ['email']; // optional
$loginUrl = $helper->getLoginUrl('http://'.$_SERVER['SERVER_NAME'].'/playground_fb.php', $permissions);
// die($_SESSION['FBRLH_' . 'state']);
echo "<script> top.location.href = '$loginUrl'; </script>";
// $permissions = ['email']; // optional
// $loginUrl = $helper->getLoginUrl('http://www.peeknchews.com/playground_fb.php', $permissions);
// echo 'LOG IN WITH FACEBOOK!';
}
else {
if(isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
else {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short lived access token for a long lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
try {
$response = $fb->get('/me?fields=email,first_name,last_name,name');
$userNode = $response->getGraphUser();
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returne d an error: ' . $e->getMessage();
unset($_SESSION['facebook_access_token']);
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returend an error: ' . $e->getMessage();
exit;
}
$fbId = $userNode->getId();
$fbEmail = $userNode->getProperty('email');
$fbName = $userNode->getName();
$fbFirstName = $userNode->getProperty('first_name');
$fbLastName = $userNode->getProperty('last_name');
$_SESSION['fbId'] = $fbId;
$_SESSION['fbEmail'] = $fbEmail;
$_SESSION['fbName'] = $fbName;
echo 'Name: ' . $fbName;
echo "<br>";
echo 'User ID: ' . $fbId . '<br>';
echo 'Email: ' . $fbEmail . '<br><br>';
echo 'Firstname : ' . $fbFirstName . '<br><br>';
echo 'Lastname : ' . $fbLastName . '<br><br>';
$image = 'https://graph.facebook.com/' . $fbId . '/picture?width=100';
echo "Picture<br><br>";
echo "<img src='$image' /><br><br> ";
//Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
echo "<script>top.location.href = 'http://www.peeknchews.com/member_index_fb.php';</script>";
}
?>
<!-- member_index_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$sessionEmail=$_SESSION['user_email'];
//facebook
$fbSessionId = $_SESSION['fbId'];
$fbSessionEmail = $_SESSION['fbEmail'];
$fbSessionName = $_SESSION['fbName'];
require_once('/home/kukkim/peeknchews.com/config.php');
?>
<li>
<a class="nav-login fancybox fancybox.iframe" href="php_login/login.php"><img src="build/img/icons/login.png">
<?php
echo "<br>";
echo $fbSessionName . "<br>";
echo $fbSessionEmail . "<br>";
$image = 'https://graph.facebook.com/' . $fbSessionId . '/picture?width=100';
echo "<img src='$image' /><br><br> ";
?>
</a>
</li>

Related

How to extract comments from a Facebook account to my Webapp?

I am trying to extract all the comments from a Facebook account to my Webapp using Graph API by coverting the result into an array and then printing the array,but the result that I am getting is a blank array followed by the respective post.I don't know whats wrong.
if (isset($accessToken))
{
echo 'Posts:';
echo '<br>';
// getting all posts published by user
try {
$posts_request = $fb->get('/me/feed');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
$total_posts = array();
$posts_response = $posts_request->getGraphEdge();
$response_array = $posts_response->asArray();
if($fb->next($posts_response)) {
$total_posts = array_merge($total_posts, $response_array);
while ($posts_response = $fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
}
foreach ($total_posts as $key) {
if(isset($key['message'])){
echo $key['message'];
echo '<br>','<br>';
}
}
//print_r($total_posts);
} else {
$posts_response = $posts_request->getGraphEdge()->asArray();
foreach ($posts_response as $key) {
if(isset($key['message'])){
echo $key['message'];
echo '<br>','<br>';
$o_id= $key['id'];
echo $o_id;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/113932879436074_129461837883178/comments',$accessToken);
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e)
{
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphEdge();
$comments=$graphNode->asArray();
print_r($comments);
}
Output:
113932879436074_131399157689446Array ( ) #TrueStory 😁
113932879436074_131393101023385Array ( ) Good afternoon Pihu Jaiswal
113932879436074_129461837883178Array ( ) Super bored
if($fb->next($posts_response)) {
$total_posts = array_merge($total_posts, $response_array);
while ($posts_response = $fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
}
foreach ($total_posts as $key) {
if(isset($key['message'])){
echo $key['message'];
echo '<br>','<br>';
}
}
//print_r($total_posts);
} else {
$posts_response = $posts_request->getGraphEdge()->asArray();
foreach ($posts_response as $key) {
if(isset($key['message'])){
echo $key['message'];
echo '<br>','<br>';
$o_id= $key['id'];
echo $o_id;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('me?fields=id,name,posts{comments{message}}');
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e)
{
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
$comments=$graphNode->asArray();
print_r($comments);
}

Facebook-PHP-SDK Getting friends list is very slow

Using below code to get friend's list ,But its takes lot of time to give the result.How can I get the result quicker.Can anyone help me to solve this issue?
or is there any other way I can get friends list?
try {
$requestFriends = $fb->get('/me/taggable_friends?fields=name&limit=100');
$friends = $requestFriends->getGraphEdge();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// if have more friends than 100 as we defined the limit above on line no. 68
if ($fb->next($friends)) {
$allFriends = array();
$friendsArray = $friends->asArray();
$allFriends = array_merge($friendsArray, $allFriends);
while ($friends = $fb->next($friends)) {
$friendsArray = $friends->asArray();
$allFriends = array_merge($friendsArray, $allFriends);
}
foreach ($allFriends as $key) {
echo $key['name'] . "<br>";
}
} else {
$allFriends = $friends->asArray();
$totalFriends = count($allFriends);
$counter = 0;
foreach ($allFriends as $key) {
echo $key['name'] . "<br>";
$counter++;
}
echo $counter;
}

Google API - get contacts mail

I want to get full contact lists with emails of my friends.
I can get the list of contacts but not having emails.
Here is my code:
require_once APPPATH . 'vendor/google/src/Google_Client.php';
require_once APPPATH . 'vendor/google/src/contrib/Google_Oauth2Service.php';
$client = new Google_Client();
$client->setApplicationName("PHP Google Test");
$client->setClientId('xxx');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://www.domain.xx/admin/others/google?test');
$client->setScopes("http://www.google.com/m8/feeds/");
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://www.domain.xx/admin/others/google';
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$val = $client->getIo()->authenticatedRequest($req);
$response = json_encode(simplexml_load_string($val->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
}
$auth = $client->createAuthUrl();
echo '' . $auth . '';
How can i get email from contacts list?
I have solution:
Code:
$response = json_encode(simplexml_load_string($val->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
Change to:
$xml = simplexml_load_string($val->getResponseBody());
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$output_array = array();
foreach ($xml->entry as $entry) {
foreach ($entry->xpath('gd:email') as $email) {
$output_array[] = array((string)$entry->title, (string)$email->attributes()->address);
}
}
print_r($output_array);
you can make use of the Contact Kind from gdata components in Google Contact APIs
Assuming $response is the response you've got from the request to Google contact:
$xml= new SimpleXMLElement($response);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
Find more info on Contact Kind element from Google Contact API refer here

Can't pass a session variable logging in with OpenID?

I tried running this code which creates a SESSION variable for the Google email but for some reason the $_SESSION['email'] does not want to store the data. I run the code and the only output I get after signing in with google is
hello world 1
hello world 2
<?php
session_start();
require 'openid.php';
$openid = new LightOpenID('http://www.splunk.com');
if (!$openid->mode)
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
else
{
if ($openid->validate())
{
//echo "Hello World!";
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
$_SESSION['email'] = $google_email;
echo $google_email."<br />";
echo "<pre>" . print_r($_GET, true) . "</pre>";
echo $_GET['contact_email'];
header("Location: " . google_login.php);
}
else
{
echo '<p>hello world 1</p>';
echo "<p>". $_SESSION['email']. "</p>";
echo '<p>hello world 2</p>';
}
}
?>
I modified the above example with the google example from the LightOpenID site but now it doesn't validate for some reason.
<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try
{
# Change 'localhost' to your domain name.
$openid = new LightOpenID('http://www.splunk.com');
if(!$openid->mode)
{
if(isset($_GET['login']))
{
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
}
else if($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
}
else
{
$attributes = $openid->getAttributes();
$google_email = $attributes['contact/email'];
echo $google_email;
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
} catch(ErrorException $e) {
echo $e->getMessage();
}

Php login that doesn't work

What's wrong in this code? I need to make users able to login to this page with facebook account with php and then ther need to be alble to see theyr albums, but login doesn't work. Where is the error in this code? Please help me! Thanks
I've tryed other login script but no one works.
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$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;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array('next'=>'http://www.photoworld.it?logoutfb'));
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://www.photoworld.it/facebooksdk/load-albums.php'));
}
?>
<?php if ($me): ?>
<?php echo "Welcome, ".$me['first_name']. ".<br />"; ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
<?php endif ?>
<?php
$user_id = $facebook->getUser();
if($user_id) {
echo $me['name'] . "<br/>";
echo $me['username']. "<br/><br/>";
$fql = "SELECT aid, name, photo_count, cover_object_id FROM album WHERE owner = '{$user_id}'";
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
$total_albums = sizeof($ret_obj);
echo $total_albums . " album totali.<br/><br/>";
for($i=0;$i<$total_albums;$i++)
{
echo "<a href='?aid=" . $ret_obj[$i]['aid'] . "'>" . $ret_obj[$i]['name'] . " (" . $ret_obj[$i]['photo_count'] . ")</a><br/>";
}
}
if (isset($_GET['aid'])) {
echo("<br/><br/>Immagini dell'album selezionato<br/><br/>");
$fql2 = "SELECT pid,src_small,src_big,src FROM photo WHERE aid=" . $_GET['aid'] . "";
$ret_obj2 = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql2,
));
$total_photos = sizeof($ret_obj2);
for($i=0;$i<$total_photos;$i++)
{
echo "<a href='" . $ret_obj2[$i]['src_big'] . "'><img src='" . $ret_obj2[$i]['src'] . "'></a><br/><br/>";
}
}
?>
$me is never really defined, so you will always get the login URL based on your conditionals. You either meant to use $user or $user_profile

Categories