Sessions

Page last updated:

Usage

PHP by default will write session data to the local disk. When your application has one instance, it’s mostly safe to use the default session storage, which is the local file system. You would only see problems if your single instance crashes as the local file system would go away and you’d lose your sessions. For many applications, this will work just fine but please consider how this will impact your application.

If you have multiple application instances or you need a more robust solution for your application, then you’ll want to use Redis or Memcached as a backing store for your session data. The build pack supports both and when one is bound to your application it will detect it and automatically configure PHP to use it for session storage.

By default, there’s no configuration necessary. Create a Redis or Memcached service, make sure the service name contains redis or memcached and then bind the service to the application.

Example:

$ cf create-service redis some-plan app-redis-sessions
$ cf bind-service app app-redis-sessions
$ cf restage app

If you want to use a specific service instance or change the search key, you can do that by setting either php.redis.session_store_service_name or php.memcached.session_store_service_name in the buildpack.yml file to the new search key. The session configuration extension will then search the bound services by name for the new session key.

Note: We are using the dot notation in configuration options from buildpack.yml, see the buildpack.yml configuration options section for details

Configuration Changes

When a Redis or Memcached service is detected, the following changes will be made to PHP’s configuration (through a php.ini snippet).

Redis

  • the redis and igbinary PHP extensions will be installed, which provides the session save handler
  • session.name will be set to PHPSESSIONID which disables sticky sessions
  • session.save_handler is configured to redis
  • session.save_path is configured based on the bound credentials, for example tcp://host:port?auth=pass

Memcached

  • the memcached, igbinary and msgpack PHP extensions will be installed, which provides the session save handler
  • session.name will be set to PHPSESSIONID which disables sticky sessions
  • session.save_handler is configured to memcached
  • session.save_path is configured based on the bound credentials (i.e. host:port)
  • memcached.sess_binary_protocol is set to On
  • memcached.sess_persistent is set to On
  • memcached.sess_sasl_username and memcached.sess_sasl_password are set with the service credentials
Create a pull request or raise an issue on the source for this page in GitHub