PHP ParseUser :: getCurrentUser (); doesn't work outside the login page
I can easily register a user and display the username.
<?php
require 'connectParse.php';
use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseSessionStorage;
session_start();
// set session storage
ParseClient::setStorage( new ParseSessionStorage() );
try {
$user = ParseUser::logIn("user", "pass");
// Do stuff after successful login.
} catch (ParseException $error) {
// The login failed. Check error to see why.
}
$currentUser = ParseUser::getCurrentUser();
$currentUser = $currentUser->get("username");
echo $currentUser;
?>
<a href="/second.php">second page</a>
But when I move the user to another page on the same server and check the current user, I get nothing.
<?php
require 'connectParse.php';
use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseSessionStorage;
session_start();
$currentUser = ParseUser::getCurrentUser();
print_r( "<p>" . $currentUser . " </p><p>is the user</p>" );
?>
These are the only code snippets both on the login and on another page. I've tried with and without session_start (); on the second page. And, of course, the user exists on another page. Spent many hours and got nowhere with this. I can hack it by setting my own SESSION variables, but I want to use the Parse functionality. I find it really just what I am missing ... would love to love any help out there! Thank you and welcome!
source to share
You need to start a session AFTER enabling the autoloader. This will avoid the problem of incomplete classes. Parse\ParseUser
wasn't loaded yet when PHP tried to deserialize the object in the session.
Call session_start();
after require '../vendor/autoload.php';
and BEFORE ParseClient::initialize('app_id', 'key', 'masterkey');
.
You don't need to use ParseClient::setStorage( new ParseSessionStorage() );
it as if the session is active. ParseClient :: initialize will use it by default.
Then you can get the current user. It's all.
source to share
This is not an answer but some kind of debugging help
Perhaps someone will find out.
I am working on this problem too. I was able to decipher the following.
The $ _SESSION data actually changes between pages.
On the login page I added print_r($_SESSION)
and also on a completely different page. The next two lines are the result of them respectively
Login page:
Array ( [parseData] => Array ( [user] => Parse\ParseUser Object ( [_sessionToken:protected] => someProtectedToken [serverData:protected] => Array ( [email] => firstName@email.com [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => firstName@email.com ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( [email] => firstName@email.com [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => firstName@email.com ) [dataAvailability:Parse\ParseObject:private] => Array ( [email] => 1 [emailVerified] => 1 [firstlast] => 1 [isBand] => 1 [sessionToken] => 1 [username] => 1 ) [className:Parse\ParseObject:private] => _User [objectId:Parse\ParseObject:private] => GVw0Thu61i [createdAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2014-12-31 21:51:26 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2014-12-31 21:51:51 [timezone_type] => 2 [timezone] => Z ) [hasBeenFetched:Parse\ParseObject:private] => 1 ) ) )
some random page:
Array ( [parseData] => Array ( [user] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => Parse\ParseUser [_sessionToken:protected] => someProtectedToken [serverData:protected] => Array ( [email] => firstName@email.com [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => firstName@email.com ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( [email] => firstName@email.com [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => firstName@email.com ) [dataAvailability:Parse\ParseObject:private] => Array ( [email] => 1 [emailVerified] => 1 [firstlast] => 1 [isBand] => 1 [sessionToken] => 1 [username] => 1 ) [className:Parse\ParseObject:private] => _User [objectId:Parse\ParseObject:private] => GVw0Thu61i [createdAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2014-12-31 21:51:26 [timezone_type] => 2 [timezone] => Z ) [updatedAt:Parse\ParseObject:private] => DateTime Object ( [date] => 2014-12-31 21:51:51 [timezone_type] => 2 [timezone] => Z ) [hasBeenFetched:Parse\ParseObject:private] => 1 ) ) )
note that [user] => __PHP_Incomplete_Class Object
Code: login.php
<?php
require '../vendor/autoload.php';
session_start();
include 'head.php';
include 'nav.php';
use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseSessionStorage;
ParseClient::initialize('a', 'b', 'c');
$name = $_POST["user"];
$password = $_POST["password"];
try {
$user = ParseUser::logIn($name, $password);
echo "you are in. hello: " .ParseUser::getCurrentUser()->get("firstlast") ;
} catch (ParseException $error) {
echo "wrong info";
}
include 'foot.php';
CODE: For a random page
<?php
require '../vendor/autoload.php';
session_start();
use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;
use Parse\ParseSessionStorage;
ParseClient::initialize('app_id', 'key', 'masterkey');
print_r($_SESSION);
?>
source to share
It looks like it might be a duplicate of this question here: User is not recognized via Parse \ ParseUser, but exists in $ _SESSION
However, usually you want the session to be created before anything else starts, so you can try moving the call session_start();
to the beginning of the file.
Example:
<?php
session_start();
require 'connectParse.php';
use Parse\ParseClient;
use Parse\ParseUser;
use Parse\ParseSessionStorage;
$currentUser = ParseUser::getCurrentUser();
print_r( "<p>" . $currentUser . " </p><p>is the user</p>" );
?>
source to share