Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ilgrim/854ae703c2c0b55ad72a0026086771c7 to your computer and use it in GitHub Desktop.
Save Ilgrim/854ae703c2c0b55ad72a0026086771c7 to your computer and use it in GitHub Desktop.
How to dynamicaly configure Redis PHP session storage on PHP7.1 for Platform.sh standard hosting
/** How to dynamicaly configure Redis PHP session storage
* on PHP7.1 for Platform.sh standard hosting
*
* Related doc:
* https://docs.platform.sh/configuration/services/redis.html
* https://www.digitalocean.com/community/tutorials/how-to-set-up-a-redis-server-as-a-session-handler-for-php-on-ubuntu-14-04
*/
# Extract Redis information
$env = getenv('PLATFORM_RELATIONSHIPS');
$relationships = json_decode(base64_decode($env), true);
$redis_host = $relationships['redis'][0]['host'];
$redis_port = $relationships['redis'][0]['port'];
$redis_path = "tcp://{$redis_host}:{$redis_port}";
# Configure redis session
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', $redis_path);
# Start session
$started = session_start();
$status = session_status();
# Output debuging information
$started_str = $started ? 'yes' : 'no';
echo "Session started: {$started_str} \n";
if($status == PHP_SESSION_DISABLED) $status_str = 'disabled';
elseif($status == PHP_SESSION_NONE) $status_str = 'none';
elseif($status == PHP_SESSION_ACTIVE) $status_str = 'active';
else $status_str = 'error';
echo "Session status: {$status_str} \n";
echo "Relationships: \n";
echo print_r($relationships);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment