mercredi 26 août 2015

Saving PHP Sessions with AWS DynamoDB

So I've been cracking at this for days now and can't seem to figure it out.

How do I save sessions on DynamoDB?

This is my code;

DB.php

    <?php
require_once(__DIR__.'/awssdk/aws-autoloader.php');
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Session\SessionHandler;

$dynamoDb = DynamoDbClient::factory(array(
    'key'    => '',
    'secret' => '',
    'region' => 'eu-west-1'
));

$sessionHandler = SessionHandler::factory(array(
    'dynamodb_client' => $dynamoDb,
    'table_name'      => 'sessions',
    'session_lifetime'         => 259200,
    'consistent_read'          => true,
    'locking_strategy'         => null,
    'automatic_gc'             => 0,
    'gc_batch_size'            => 25,
    'max_lock_wait_time'       => 15,
    'min_lock_retry_microtime' => 5000,
    'max_lock_retry_microtime' => 50000,
));
$sessionHandler->register();
//$sessionHandler->createSessionsTable(5, 5);

session_id("careers");
session_start(); //start session


$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "password";
$db['db_name'] = "dbname";

foreach($db as $key => $value) {
    define(strtoupper($key), $value);
}

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Login check function
function loggedInCareers() {
    if (isset($_SESSION['careers']))
        $loggedIn = true;
    return $loggedIn;
}


?>

login.php

// Lets start php sessions.
    session_start();

    $_SESSION['careers'] = array( 
        "username" => $_POST['username'],
        "password" => $_POST['password'],
        "firstname" => $row ['user_firstname'],
        "lastname" => $row ['user_lastname'], 
        "role" => $row ['user_role']
    );

logout.php

<?php

include($_SERVER["DOCUMENT_ROOT"] . "/careers/php/db.php");

global $connection;
session_start();
unset($_SESSION['careers']);
//setcookie("rememberblog", $email, time()-604800, "/");

header("Location: ../careers-admin-login.php");

?>

Everything works that is I register a session, log in, and log out. The problem is that only one user can loginenter image description here

Once a user logs in, logon data is added to the table below and thats it. PS I have 3 sections to my site thats why I have named them (web, blog, careers). How can I set this up so as many people accessing my site can have sessions saved in the table.

enter image description here

I have tried accessing my site from multiple devices. Thanks




Aucun commentaire:

Enregistrer un commentaire