This tutorial shows how to handle sessions on App Engine.
Many apps need session handling for authentication and user preferences. PHP comes with a memory-based implementation to perform this function. However, this implementation is unsuitable for an app that can be served from multiple instances, because the session that is recorded in one instance might differ from other instances.
Objectives
- Write the app.
- Run the app locally.
- Deploy the app on App Engine.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Firestore API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Firestore API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- Configure the gcloud CLI to use your
new Google Cloud project:
# Configure gcloud for your project gcloud config set project PROJECT_ID
ReplacePROJECT_ID
with the Google Cloud project ID that you created or selected previously.
Setting up the project
In your terminal window, start in a directory of your choosing and create a new directory named
sessions
. All of the code for this tutorial is inside thesessions
directory.Change into the
sessions
directory:cd sessions
Install the dependencies:
composer require google/cloud-firestore
At the end of this tutorial, the final file structure is similar to the following:
sessions
├── app.yaml
└── composer.json
├── index.php
Writing the app
In your terminal window, create a file called
index.php
with the following content:
This app displays greetings in different languages for every user. Returning users are always greeted in the same language.
Before your app can store preferences for a user, you need a way to store information about the current user in a session. This sample app uses Firestore to store session data.
You can use the Firestore session handler
to use Firestore for PHP sessions.
The following diagram illustrates how Firestore handles sessions for the App Engine app.
After you’ve set session_set_save_handler
, every request has a $_SESSION
global variable that you can use to access the session. The session data is
stored in Firestore.
Running locally
Start the built-in PHP web server:
php -S localhost:8080
View the app in your web browser:
Cloud Shell
In the Cloud Shell toolbar, click Web preview and select Preview on port 8080.
Local machine
In your browser, go to
http://localhost:8080
You see one of five greetings: “Hello World”, “Hallo Welt”, "Hola mundo”, “Salut le Monde”, or “Ciao Mondo.” The language changes if you open the page in a different browser or in incognito mode. You can see and edit the session data in the Google Cloud console.
To stop the HTTP server, in your terminal window, press
Control+C
.
Deploying and running on App Engine
You can use the App Engine standard environment to build and deploy an app that runs reliably under heavy load and with large amounts of data.
This tutorial uses the App Engine standard environment to deploy the server.
In your terminal window, create an
app.yaml
file and copy the following:Deploy the app on App Engine:
gcloud app deploy
To view the live app, enter the following URL:
https://PROJECT_ID.REGION_ID.r.appspot.com
Replace the following:
PROJECT_ID
: Your Google Cloud project IDREGION_ID
: A code that App Engine assigns to your app
The greeting is now delivered by a web server running on an App Engine instance.
Deleting sessions
PHP performs session garbage collection (GC),
which removes old and expired sessions
according to your php.ini
configuration. The Firestore session
handler doesn't clean up sessions by default, but you can configure it to do so
by passing a gcLimit
option when creating the session handler:
Sessions are removed using probability based session GC, or when you call the
session_gc
function explicitly.
Debugging the app
If you cannot connect to your App Engine app, check the following:
- Check that the
gcloud
deploy commands successfully completed and didn't output any errors. If there were errors (for example,message=Build failed
), fix them, and try deploying the App Engine app again. In the Google Cloud console, go to the Logs Explorer page.
In the Recently selected resources drop-down list, click App Engine Application, and then click All module_id. You see a list of requests from when you visited your app. If you don't see a list of requests, confirm you selected All module_id from the drop-down list. If you see error messages printed to the Google Cloud console, check that your app's code matches the code in the section about writing the web app.
Make sure that the Firestore API is enabled.
Clean up
Delete the project
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the App Engine instance
- In the Google Cloud console, go to the Versions page for App Engine.
- Select the checkbox for the non-default app version that you want to delete.
- To delete the app version, click Delete.