3

I can log the user in just fine, 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 navigate the user to another page on the same server, and check for a 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>" );

?>

Those are the only pieces of code on both the login and other page. I've tried with and without the session_start(); on the second page as well. And of course, the user does exist on the other page. Spent a good amount of hours and haven't gotten anywhere with this. I can hack around it setting my own SESSION variables but I want to use the functionality of Parse. I figure it is something really simple that I am missing... would love love love any help out there! Thank you and cheers!

2
  • Hello, You can set "session.auto_start" to "1" in php.ini instead of calling session_start each time. Or you can only put session_start at the top of file before "require 'connectParse.php'". Thanks Commented Oct 15, 2014 at 8:52
  • Hey Thomas, I'm facing the exact same issue - did you get it resolved? Commented Nov 18, 2014 at 10:52

3 Answers 3

5
+50

You need to start the session AFTER have included the autoloader. This to avoid the problem of incomplete classes. The Parse\ParseUser had not been 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() ); as if the session is active ParseClient::initialize will use it by default.

Then you could get the current user. That's all.

Sign up to request clarification or add additional context in comments.

2 Comments

I did what you said and am still experiencing the same problem. Should the initialize be present with every page? I have updated my "Answer"
Ignore my comment above. You are right. It was a problem with the session issue. To whomever runs into this problem again. Check your php.ini and make sure that session.auto_start is not enabled (1). Even though I did what @Laxus said, it still ran into the same problem until I turned that off.
2

This is not an answer but some debugging help

Maybe someone will figure it out

I am also running to this issue. I have been able to decipher the following.

The $_SESSION data is actually changing between page.

On the login page I have added print_r($_SESSION) as well as on a completely different page. The following two lines are the results of them respectively

Login Page:

Array ( [parseData] => Array ( [user] => Parse\ParseUser Object ( [_sessionToken:protected] => someProtectedToken [serverData:protected] => Array ( [email] => [email protected] [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => [email protected] ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( [email] => [email protected] [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => [email protected] ) [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] => [email protected] [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => [email protected] ) [operationSet:protected] => Array ( ) [estimatedData:Parse\ParseObject:private] => Array ( [email] => [email protected] [emailVerified] => 1 [firstlast] => firstName lastName [isBand] => [username] => [email protected] ) [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 the 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);

?>

1 Comment

Put session_start(); after require '../vendor/autoload.php'; and before ParseClient::initialize
0

This looks like it might be a duplicate of this question here: User not recognized through Parse\ParseUser, but exists in $_SESSION

However, usually you want the session to be instantiated before anything else runs, so you could try moving the session_start(); call to the top of your 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>" );

?>

2 Comments

As you can see in the note of the original poster: "I've tried with and without the session_start();" I personally have tested it on top and below. The issue seems to be that the _SESSION data actually changes between the page. I will try to edit his post.
Laxus hits on the issue below and more can be read here: stackoverflow.com/a/2010505/2930828

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.