parse.com sessions in php-sdk - php

I am new in WordPress and PHP. I am using php-sdk of parse.com for developing my plugin. My php-sdk folder is inside my plugin folder. I have stored App ID , Rest API key and Master key in my database of WordPress.
For Parse initialization, I required App ID , Rest API key and Master key. I can fetch it from my WordPress database.
After parse initialization, I can fetch my data from parse. For fetching data you have to authenticate first. username and password are required to authentication. I am getting these username and password by a form. When I am submitting my form then I am initializing the parse and then authenticating it. I can fetch the data on same page as well. But I am going to other page then I can not get the data. I try to use following code on other page :
session_start();
require 'vendor/autoload.php';
use Parse\ParseUser;
$currentUser = ParseUser::getCurrentUser();
if ($currentUser) {
// do stuff with the user
echo "username: ". $currentUser->getObjectId();
} else {
// show the signup or login page
echo "Not Found";
}
I could not get my current user. How can I maintain the sessions in parse.com. I want to initialize and authenticate the users once for all. After that I can get the data from any of the page of my WordPress. How to solve this problem ? Can any body help me ??

After session_start() use:
\Parse\ParseClient::initialize(....);
\Parse\ParseClient::setStorage(new \Parse\ParseSessionStorage());

Related

Guzzle: How to save cookies to use on another php file

I used Guzzle lib for my project.
I use guzzle to log in to a website (domain.com/login.php) and then I can get another (domain.com/post.php) as a logged in user.
The question is "How I can use cookies to load (domain.com/post.php) many requests as I want without log in the website again?"
My mine is something like that:
login = check cookies saved
if ( login ) then get domain.com/post.php
else
log in again, then get domain.com/post.php
Many thanks.
Simply Like,
PHP Sessions
PHP Cookies
Login.php
<?php
session_start();
if(login_success){
$_SESSION["active_user"] = "user-ID or Token";
}else{
//show login error
}
?>
Post.php
<?php
session_start();
if($_SESSION['active_user']){
// Check ID or Token to validate User
}else{
// redirect to login page
}
?>

Steam auth login api

When I login on one page with the steam auth login I can use all the infomation from the $steamprofile array, but when I change to another php file I dont have accses to the same $steamprofile array, how do I get this to work? I have tried to setup session_start(); on both files but diden't work.
If you login using the SteamAPI you get a response with all the data which can be viewed on one page, if you want it to be saved in a session you need to save this data in $_SESSION array in PHP.
For example:
$_SESSION['steamid'] = $steamprofile['steamid'];
echo $_SESSION['steamid']; // on a different page
You then need to include session_start() on all pages you want to share those information with. Read more about PHP session here.

Get user ID using Facebook PHP SDK 4

Following this discussion i can't figure it out how to getUserId from session or how to create session from a signed request in new SDK 4 for PHP? I'm trying to migrate my apps from old SDK to new one and my code doesn't work well.
I'm trying to get the user ID on page tab where i'm an admin. I've tried to getSession from FacebookPageTabHelper with no luck.
My php code:
session_start();
require_once '../api/facebook/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookPageTabHelper;
FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
$pageHelper = new FacebookPageTabHelper();
$page = $pageHelper->getPageId();
$admin = $pageHelper->isAdmin();
$fan = $pageHelper->isLiked();
$appdata = $pageHelper->getSignedRequest()->payload['app_data'];
$session = $pageHelper->getSession();
$user = $session->getUserId(); // always empty
Facebook API doesn't provide you with this information anymore (if you use the user access token). You should get the ID of the user by looking at his posts or his comments, and even by this way you need to guess that he is a public user otherwise it will not work.
If you can login with the app token it should work but it's not easy to get confirmation for your application by Facebook.

PHP SDK + Code Igniter

First, I downloaded the Facebook PHP SDK and applied the first answer in here:
Using Facebook PHP-SDK 3.x to register/login user with Codeigniter 2.1.0
In my controller:
I try to get the user: $this->facebook->getUser(); .. If it's 0, I call another function $this->facebook_login(); - else I save some data in the database like name for example.
In facebook_login(), a Login Url is generated: $this->facebook->getLoginUrl() - and is passed to the view, which displays it.
So far I click on the login url and it redirects to Facebook, asks for approval and authentication and so on - then redirects me back to the page in "1".
The problem is, getUser is returning zero. I printed the $SESSION variable and found out that only fb{appId}_state is being set.
I tried creating another php file (login.php) that is not in the CI folder with the following code:
<?php
define('BASEPATH', '');
require_once('application/libraries/facebook.php');
$fb = new Facebook(array('appId'=>'...', 'secret'=>'...'));
$user = $fb->getUser();
if($user == 0){
$login = $fb->getLoginUrl();
echo 'Login';
} else {
print_r($fb->api('/me'));
}
?>
If I visit login.php then visit the page in "1" above - getUser returns the user id and $_SESSION contains the user details.Therefore, I guess the problem is with CodeIgniter but I am not sure how to solve it.
I searched and tried several solutions but that's the best I could do. If possible, I don't want to use the JS SDK.
Thank you
I found a solution and wrote a blog post about it here.

CodeIgniter - Is my custom session data being stripped by Facebook?

I'm wondering if there's a way to dump all of the values of
$this->session->userdata()
so I can troubleshoot?
I'm working within Facebook, and have a login page, and once that's successful I want to pass around the UID of the current user, and I thought this would work well.
I currently have the uid set as follows:
require_once 'facebook.php';
$appapikey = 'XXXX';
$appsecret = 'XXXX';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->require_login();
$this->db->like('uid', $user_id);
$this->db->from('users');
$has_signed_up = $this->db->count_all_results();
if ($has_signed_up == 0) {
redirect('/setup/signup/', 'location');
}
else {
$this->session->set_userdata('uid', $user_id);
redirect('/preferences/index/', 'location');
}
So the redirection occurs, and I have a very simple setup over at preferences/index:
echo "this is the preferences form <br />";
echo $this->session->userdata('uid');
echo $this->session->userdata('session_id');
And the result is an inscrutable:
this is the preferences form
858f500e167e359edc1942a96f3bac35
So it totally skips over the middle echo containing the uid. Am I not setting this correctly? Is there a way to dump all values of the session array to see what's getting through? Any help would be just great.
UPDATE
I have run var_dump($this->session->userdata) on each the raw website and through Facebook.
On the website it exposes all set values in an array containing 5 values (session_id, IP, User_agent, last_activity, and uid).
Within the Facebook chrome however, it only shows the 4 values set by CodeIgniter. I've heard cookies can only be 4k and that encryption could be a problem. Could FB be filling up cookies with its own encrypted (read:larger) data?
UPDATE 2
When I comment out the redirect, and just have:
else {
$this->session->set_userdata('uid', $user_id);
echo ':test_'.$this->session->userdata('uid').'_test:';
//redirect('/preferences/index/', 'location');
}
It dutifully returns :test_1234_test: within Facebook. So somewhere during the redirect it's losing this part of the array (but not the whole array).
Is it possibly just creating a new session on the redirect page? So that's why it only has the four "stock" variables? If this is the case, I'll need to research how it creates the sessions, and if Facebook clears cookies I suppose.
UPDATE 3
So I've turned to using a DB to store session information instead of cookies, thinking FB was either stripping them or colliding with them. I currently have the app set up to
Set $user_id = 1234
$this->session->set_userdata('uid', $user_id)
Redirect to the new page
Var_dump all possible information
What occurs in the DB is this:
DB records http://nikolausjj.facebook.joyent.us/Picture2.png
So it creates one good record, with the user data, then immediately upon the redirect creates a new session without recognizing the prior one. Can someone explain where the CI framework checks to see if it has a prior session existing? The user manual explains it as "magic" basically.
You can use var_dump() to output the session. Something like this
var_dump($this->session);
The set_userdata call looks ok. Are you sure $user_id is set. Because the echo is surley executed but uid isn't set or set to empty string.
Try replacing the echo with
echo ':test_'.$this->session->userdata('uid').'_test:';
Other information helpful for answering
What browser are you using?
Do you have an underscore _ in your domain name?
Are you using CI sessions or some wrapper for native PHPsessions
Is the value for uid also lost/not set when you comment out the redirect?
Other suggestions:
try redirect('/preferences/index/', 'refresh'); instead of location
I'm not familiar with facebook development but is /preferences/index under your control? If yes try removing (if present) $this->load->library(‘session’) and instead load it in autoload.php.
try changing $config[‘sess_match_ip’] to `FALSE
try setting $config[‘sess_encrypt_cookie’] to FALSE
try replacing the use of CI-Session with CI Native session
Is UID sensible information if not store it in a cookie. If it matters if it can be spoofed don't.
I didn't solve how to pass session variables from one page to another via Facebook. My solution was simply to call the Facebook API for the user's UID again on each page. Not great programming, but it works alright for me. :-/

Categories