parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
811 stars 346 forks source link

ParseUser::getCurrentUser() not set across different php pages #346

Closed jenlai1345 closed 7 years ago

jenlai1345 commented 7 years ago

Hi,

I'm a newbie in php. I've only worked with the ios/android parse sdk extensively in the past. Recently I built a php site, with the following file structure:

my_app

every page can navigate to other pages. However, I'm encountering the problem that my ParseUser.getCurrentUser() does not persist when I go to a different page. For example, after the user successfully logs in, it redirects to index.php and under that page the user isn't recognized as logged-in.

My question is, what's the correct place to call ParseClient.initialize('x','xx','xxx') ?
Right now, on top of EACH of my php page, I have: `<?php require 'vendor/autoload.php';

session_start();

use Parse\ParseObject; use Parse\ParseQuery; use Parse\ParseACL; use Parse\ParsePush; use Parse\ParseUser; use Parse\ParseInstallation; use Parse\ParseException; use Parse\ParseAnalytics; use Parse\ParseFile; use Parse\ParseCloud; use Parse\ParseClient;

$parse_server_url = 'http://xxxxx.elasticbeanstalk.com'; $app_id = 'xxxxx'; $rest_key = 'xxxxxxx'; $master_key = 'xxxxx';

ParseClient::initialize( $app_id, $rest_key, $master_key ); ParseClient::setServerURL($parse_server_url, 'parse');

// start doing stuff ?>`

My suspicion is that I "shouldn't" call initialize in every page..... but if I don't, it throws an exception.

Any help is greatly appreciated!

montymxb commented 7 years ago

@jenlai1345 from what I can tell it looks like your code is pretty much spot on! Internally the php sdk utilizes sessions if they are available for persistence, otherwise it's just memory based storage and the next load wouldn't have your user.

In your case you're calling session_start() before you call ParseClient::initialize, which is correct. Doing it that way ensures that your session is setup before the sdk is setup, allowing the sdk to detect and use session storage.

It's very important that you do initialize on every page load. Initializing the sdk will not persist across page loads unless you've set it up yourself. This is done enough that often it's helpful to put your initializing code into some external file (like setup-parse.php), which you can require to automatically setup a session, initialize the sdk, setup the server url and do any other preparatory work you need before the sdk is considered good to go 👍 .

As for the fact your user isn't persisting across pages, that's a bit more troubling... For starters If the session is properly setup you should be able to check $_SESSION after logging in to verify your user is present (you would see it under $_SESSION['parseData']['user']).

jenlai1345 commented 7 years ago

Thank you so much @montymxb ! Thank you for giving me a overall picture with a thorough explanation on how it works. With your suggestion, I put all my Parse initialization codes (and session_start()) into a connectParse.php file and include it at the very beginning of each of my php page. I was finally able to get it to work : )

Thank you again for your help!!! You're amazing!