Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Last active August 29, 2015 14:13
Show Gist options
  • Save tristanlins/1570d80741130c2fe888 to your computer and use it in GitHub Desktop.
Save tristanlins/1570d80741130c2fe888 to your computer and use it in GitHub Desktop.
Use symfony memcached session handler in Contao
<?php
...
/**
* Symfony session storage
*/
$memcached = new \Memcached();
$memcached->addServer($GLOBALS['TL_CONFIG']['sessionMemcachedHost'], $GLOBALS['TL_CONFIG']['sessionMemcachedPort']);
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/SessionBagInterface.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/MetadataBag.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/Handler/MemcachedSessionHandler.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php';
require TL_ROOT . '/composer/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php';
register_shutdown_function(function() {
\Session::getInstance()->__destruct();
});
new \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage(
[],
new \Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler(
$memcached,
['prefix' => $GLOBALS['TL_CONFIG']['sessionMemcachedPrefix'] . $GLOBALS['TL_CONFIG']['encryptionKey'] . '_']
)
);
/**
* Start the session
*/
@session_set_cookie_params(0, (TL_PATH ?: '/')); // see #5339
@session_start();
...
<?php
### INSTALL SCRIPT START ###
$GLOBALS['TL_CONFIG']['sessionMemcachedHost'] = 'localhost';
$GLOBALS['TL_CONFIG']['sessionMemcachedPort'] = 11211;
$GLOBALS['TL_CONFIG']['sessionMemcachedPrefix'] = 'contao_session_';
### INSTALL SCRIPT STOP ###
@tristanlins
Copy link
Author

Now it is configurable, put these three options into your localconfig.php and update the init script. Then you can change the settings from your localconfig.php. This may be helpful, when using syncCto or something similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment