Skip to content

Commit

Permalink
Merge pull request #2 from azophy/setup_redis_session_driver
Browse files Browse the repository at this point in the history
Setup redis session driver
  • Loading branch information
azophy authored Aug 23, 2022
2 parents 871d9fe + 293642b commit 497ddd4
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# should be replaced by value from `php artisan key:generate --show`
APP_KEY=1234
APP_DEBUG=true
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ PHP by default provided a native [Session handling mechanism](https://www.php.ne

All services managed using docker compose

# Setup
## Setup
- `docker compose up`
- website could be accessed at http://localhost:8000

## Plan

From my limited research, it seems that there are a couple of steps required to achieve this goal:
- use the same backend for session driver in each frameworks, here we use Redis -> done
- use the same Redis key format -> should be configurable, need to make sure
- laravel has `database.redis.prefix` config var which could be changed to edit its Redis prefix key which would be used to store session key
- use the same cookie variable name -> should be configurable, need to make sure
- laravel has `session.cookie` config var which could be changed to edit its cookie variable name
- use the same serialization method -> require considerable configuration

## Alternative

If its later revealed that the above steps is impossible or prohibitively difficult, than here I list a couple alternative solutions:
- use OAuth 2.0 or OpenID Connection to achieve synced user session
4 changes: 4 additions & 0 deletions codeigniter/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ FROM php:7.2-apache

RUN a2enmod rewrite

# Install PHP extensions
# ref: https://stackoverflow.com/a/63579640/2496217
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions redis
9 changes: 7 additions & 2 deletions codeigniter/application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,15 @@
| except for 'cookie_prefix' and 'cookie_httponly', which are ignored here.
|
*/
$config['sess_driver'] = 'files';
// redis driver
$config['sess_driver'] = 'redis';
$config['sess_save_path'] = 'tcp://redis:6379'; // ref: https://codeigniter.com/userguide3/libraries/sessions.html#redis-driver

// file driver
//$config['sess_driver'] = 'files';
//$config['sess_save_path'] = sys_get_temp_dir(); // ref: https://stackoverflow.com/a/33898705
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = sys_get_temp_dir(); // ref: https://stackoverflow.com/a/33898705
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Expand Down
13 changes: 10 additions & 3 deletions codeigniter/application/controllers/Sesi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public function index()
{
if (!$this->session->random_value)
$this->session->random_value = random_string('alnum', 16);
$output = json_encode([
'random_value' => $this->session->random_value,
'dat_time' => date('Y-m-d H:i:s'),
'session_id' => $this->session->session_id,
'reset_url' => site_url('sesi/reset'),
'session_data' => $this->session,
]);

echo "Random value in session: " . $this->session->random_value;
echo "<br/>Date Time: " . date('Y-m-d H:i:s');
echo "<br/><a href='" . site_url('sesi/reset') . "' target='_blank'>Reset value</a>";
return $this->output
->set_content_type('application/json')
->set_output($output);
}

public function reset()
Expand Down
2 changes: 2 additions & 0 deletions codeigniter/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
exit(1); // EXIT_ERROR
}

ini_set('session.serialize_handler', 'php_serialize');

/*
*---------------------------------------------------------------
* SYSTEM DIRECTORY NAME
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ services:
volumes:
- ./laravel:/app
#command: php artisan serve --host 0.0.0.0 --port 8000

redis:
image: redis:alpine
6 changes: 3 additions & 3 deletions laravel/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# ref: https://stackoverflow.com/a/63579640/2496217
#COPY --from=registry.digitalservice.id/proxyjds/library/mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
#COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
# Install PHP extensions
RUN install-php-extensions redis

WORKDIR /app

# Install PHP extensions
#RUN install-php-extensions ldap

# ini gak bisa dibuat copy composer aja karena di composer install ada tahap eksekusi artisan. kalau di copy hanya file composer, file artisannnya tidak ada dan jadinya composer installnya gagal
COPY . /app
Expand Down
5 changes: 3 additions & 2 deletions laravel/config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|
*/

'default' => env('CACHE_DRIVER', 'file'),
'default' => env('CACHE_DRIVER', 'redis'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -105,6 +105,7 @@
|
*/

'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),
//'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),
'prefix' => 'ci_session',

];
5 changes: 3 additions & 2 deletions laravel/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@

'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
//'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
'prefix' => '',
],

'default' => [
'url' => env('REDIS_URL'),
'url' => env('REDIS_URL', 'tcp://redis:6379'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
Expand Down
17 changes: 11 additions & 6 deletions laravel/config/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
|
*/

'driver' => env('SESSION_DRIVER', 'file'),
'driver' => env('SESSION_DRIVER', 'redis'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -72,7 +72,7 @@
|
*/

'connection' => env('SESSION_CONNECTION'),
'connection' => env('SESSION_CONNECTION', 'default'),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -126,10 +126,11 @@
|
*/

'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
),
'cookie' => 'ci_session',
//'cookie' => env(
//'SESSION_COOKIE',
//Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
//),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -198,4 +199,8 @@

'same_site' => 'lax',

// undocumented settings to change Laravel's Session Storage serialization strategy
// ref; https://github.com/laravel/framework/pull/40595
'serialization' => 'php',

];
7 changes: 7 additions & 0 deletions laravel/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
*/

Route::get('/', function () {
session([
'random_laravel_value' => \Illuminate\Support\Str::random(16)
]);

return response()->json([
'random_value_in_sesson' => session('random_value'),
'random_value_by_laravel' => session('random_laravel_value'),
'session_id' => session()->getId(),
'session_data' => session()->all(),
]);
});

0 comments on commit 497ddd4

Please sign in to comment.