The DynamoDB Session Handler is a custom session handler for PHP that allows developers to use Amazon DynamoDB as a session store. Using DynamoDB for session storage alleviates issues that occur with session handling in a distributed web application by moving sessions off of the local file system and into a shared location. DynamoDB is fast, scalable, easy to setup, and handles replication of your data automatically.
Setting up:
1. Make sure you have PHP >= 5.5.0 2. install AWS PHP SDK(v3) from here http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html 3. Configure PHP SDK to use any of the credentials options as mentioned here: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html 4. See more details about DyanmoDB provided session handler here: https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/dynamodb-session-handler.html 5. A DynamoDB table to store session info, with 'id' (String) as Hash key.End to End PHP code with debug turned on:
require ‘vendor/autoload.php’; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\SessionHandler;
$sdk = new Aws\Sdk([ ‘region’ => ‘us-west-2’, ‘version’ => ‘latest’, ‘http’ => [ ‘debug’ => true ] ]);
$dynamodb = $sdk->createDynamoDb();
//var_dump($dynamodb);
$sessionHandler = SessionHandler::fromClient($dynamodb, [ ‘table_name’ => ‘usersession’ ]);
$sessionHandler->register();
// Start the session session_start();
// Alter the session data $_SESSION[‘user.name’] = ‘jeremy’; $_SESSION[‘user.role’] = ‘admin’;
// Close the session (optional, but recommended) session_write_close();
echo “successfully connected”;
?>
php sessionProvider.phpsuccessfully connected
Now, check the DynamoDB table if the session information is stored successfully.
Here is the example structure(DynamoDB JSON format):
{
"data": {
"S": "user.name|s:6:\"jeremy\";user.role|s:5:\"admin\";"
},
"expires": {
"N": "1447886783"
},
"id": {
"S": "PHPSESSID_5elf6mc7kmbjec96kn9q07q9i5"
}
}