phpgoogle-app-enginegoogle-cloud-platformgoogle-cloud-datastoredev-appserver

Hosting project with dev_appserver.py gives different results to deploying to Google Cloud


The project is meant to take some user input and check the connected Google Datastore for a match. When hosted using dev_appserver.py everything works as intended.

This is not the case for the live version, uploaded with gcloud app deploy. The problem occurs when trying to retrieve an entity, using its key for a lookup.

<?php
require __DIR__ . "/vendor/autoload.php";

use Google\Cloud\Datastore\DatastoreClient;

$datastore = new DatastoreClient([
"projectId" => "54327567209-project"
]);

// Retrieving user entities
function lookup_user(DatastoreClient $datastore, string $id) {
  $key = $datastore->key("user", $id);
  $user = $datastore->lookup($key);
  return $user;
}

// Validation
$authErr = "";
$id = $pword = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Existence check
  if (!empty($_POST["id"]) && !empty($_POST["password"])) {
    $id = $_POST["id"];
    $pword = $_POST["password"];
    // Attempt to get user from Google Cloud
    $user = lookup_user($datastore, $id); // <---- Error happens here
    // Check if user exists and password matches
    if (!empty($user["name"]) && $user["password"] == $pword) {
      // Removed for debugging
    } else {
      // Id or password authentication fail
      $authErr = "User id or password is invalid";
    }
  } else {
    // Missing id or password
    $authErr = "User id or password is invalid";
  }
}
?>

<!DOCTYPE html>
<html>
  <head>
    <style>
      .error {color: #FF0000;}
    </style>
  </head>
  <body>
    <h2>Account Access</h2>
    <form method="post" action="">
      <div>Username: <input name="id" type="text"></input></div>
      <div>Password: <input name="password" type="password"></input></div>
      <div><input type="submit" value="Login"></div>
      </br>
      <span class="error"><?php echo $authErr; ?></span>
    </form>
  </body>
</html>

The page goes blank when it tries to do the lookup, similar to running exit(). There's no error message that I can see. I'm wondering if there's some kind of logging I can turn on to help with this.


Solution

  • The problem was with the lookup_user() function. Removing this function and pasting the same code inline fixed the issue.