2

So I'm using Parse.com with PHP for my user system but it doesn't work as expected. When I call the signUp() method the new user is successfully saved in Parse DB and is logged in but if I navigate to another page ParseUser::getCurrentUser() doesn't seem to fetch the current user. The same thing happens with the ParseUser::logIn() method. Here's part of my code:

<?php

            require 'vendor/autoload.php'; 

            use Parse\ParseClient;

            ParseClient::initialize('*, *, *');

            use Parse\ParseUser;
            use Parse\ParseException;
            use Parse\ParseSessionStorage;

            function showForm() {
                echo "
                <form action='user.php' method='POST'>
                    Όνομα (χωρίς #)<br />
                    <input type='text' name='name' required><br /><br />
                    Κωδικός πρόσβασης<br />
                    <input type='password' name='password' required><br /><br />
                    <input type='submit' name='login' value='Σύνδεση'> <input type='submit' name='register' value='Εγγραφή'>
                </form>
                ";
            }
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                if (isset($_POST['login'])) {
                    try {
                        $user = ParseUser::logIn($_POST['name'], $_POST['password']);
                        echo "<a href='add.php'>Continue</a>";
                        $currentUser = ParseUser::getCurrentUser();
                        echo $currentUser->getUsername();
                    } catch (ParseException $error) {
                        echo "Error: " . $ex->getCode() . " " . $ex->getMessage();
                        showForm();
                    }
                } else if (isset($_POST['register'])) {
                    $user = new ParseUser();
                    $user->set("username", $_POST['name']);
                    $user->set("password", $_POST['password']);

                    try {
                        $user->signUp();
                        echo "Εγγραφήκατε επιτυχώς!";
                        $currentUser = ParseUser::getCurrentUser();
                        echo $currentUser->getUsername();
                    } catch (ParseException $ex) {
                        echo "Error: " . $ex->getCode() . " " . $ex->getMessage();
                        showForm();
                    }
                }
            } else {
                showForm();
            }
        ?>
5
  • When you say "doesn't seem to fetch the current user", does it fetch anything? Note that it's not likely to be relevant but you should really be terminating your input fields, e.g. <input type='text' name='name' required />. It's currently not valid HTML. Commented Mar 25, 2015 at 1:01
  • How would I check? The script dies at that point. Commented Mar 25, 2015 at 16:48
  • In that case, add a general catch after catch (ParseException $error) and see what went wrong. Commented Mar 25, 2015 at 21:42
  • I'm afraid it doesn't catch anything. I told you, the registration/login gets successfully done but the issue is the session. Commented Mar 27, 2015 at 13:36
  • Here is a more detailed explanation stackoverflow.com/questions/25347123/… Commented Jan 26, 2017 at 11:07

1 Answer 1

1

The problem was that I forgot to put session_start(); after require 'vendor/autoload.php'; in every page I needed the session.

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

Comments

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.