Using and Configuring Zend Session and Zend Cache Memcached - Zend Framework 2.3 - session

Actually, I'm using "standard" sessions manager config:
http://framework.zend.com/manual/current/en/modules/zend.session.manager.html
I want to use cache and save my session's data into server's cache (memcached) for improves performances and scalability.
I set php.ini like this (localhost memcached):
session.save_handler=memcached
session.save_path= "tcp://127.0.0.1"
and it show this error:
Warning: session_start(): Cannot find save handler 'memcached' - session startup failed in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\SessionManager.php on line 98
So, I don't understand how to configure my config/autoload/global.php and module/application/module.php. it's my first time that I want to implement memcached and caching in general. thanks, so much!
I tried to modify module/application/module.php like this:
---add session and cache ---
use Zend\Session\Config\SessionConfig;
use Zend\Session\Container;
use Zend\Cache\StorageFactory;
use Zend\Session\SaveHandler\Cache;
use Zend\Session\SessionManager;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Session\Validator\RemoteAddr;
--- end session and cache ---
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initSession(array(
'remember_me_seconds' => 180,
'use_cookies' => true,
'cookie_httponly' => true,
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
'phpSaveHandler' => 'memcached',
'savePath' => 'tcp://127.0.0.1',
)
));
}
public function initSession($config)
{
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->getValidatorChain()
->attach(
'session.validate',
array(new HttpUserAgent(), 'isValid')
)
->attach(
'session.validate',
array(new RemoteAddr(), 'isValid')
);
$cache = StorageFactory::factory(array(
'adapter' => array(
'name' => 'memcached',
'options' => array(
'server' => '127.0.0.1',
),
)
));
$saveHandler = new Cache($cache);
$sessionManager->setSaveHandler($saveHandler);
$sessionManager->start();
Container::setDefaultManager($sessionManager);
}
but it shows this error:
Warning: ini_set() expects parameter 2 to be string, array given in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\Config\SessionConfig.php on line 88
Fatal error: Call to undefined method Zend\Stdlib\CallbackHandler::attach() in C:\Program Files (x86)\xampp\htdocs\Zend-application\module\Application\Module.php on line 68
this is my config/autoload/global.php
return array(
'db' => array(
'driver' => 'Pdo_Mysql',
'charset' => 'utf-8',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'session' => array(
'config' => array(
'class' => 'Zend\Session\Config\SessionConfig',
'options' => array(
'name' => 'zend-application',
),
),
'storage' => 'Zend\Session\Storage\SessionArrayStorage',
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
),
);

Hoping it'll help someone, I resolved my issue. I'm working in Win7 enviroment and memcached doesn't work on it! I changed :
session.save_handler=memcached
session.save_path= "tcp://127.0.0.1"
to
session.save_handler=memcache
session.save_path= "tcp://127.0.0.1:11211"
I restored the "standard" session manager config and memcache works correctly. When I'll transfer the entire site to apache server, I'll change php.ini for using memcached.
http://framework.zend.com/manual/current/en/modules/zend.session.manager.html

Related

How to echo config item from config file for view in Codeigniter?

How to call config item from config file for view in codeigniter.
here is my config file
$config['user'] = array(
'email_validation' => 'email validation',///^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
'no_permission' => '/',
'admin_group' => 'admin',
'default_group' => 'default',
'public_group' => 'public',
'users' => 'aauth_users',
'groups' => 'aauth_groups',
'user_to_group' => 'aauth_user_to_group',
'perms' => 'aauth_perms',
'perm_to_group' => 'aauth_perm_to_group',
'perm_to_user' => 'aauth_perm_to_user',
'pms' => 'aauth_pms',
'system_variables' => 'aauth_system_variables',
'user_variables' => 'aauth_user_variables',
'remember' => ' +3 days',
'max' => 13,
'valid_chars' => array(' ', '\''),
'ddos_protection' => true,
'recaptcha_active' => false,
'recaptcha_login_attempts' => 4,
'recaptcha_siteKey' => '',
'recaptcha_secret' => '',
'max_login_attempt' => 10,
'verification' => false,
'email' => 'admin#admin.com',
'name' => 'Emre Akay'
);
Here is my load config
$this->config->load('user');
And I will view its item for view as below
$site_name = $this->config->item('email_validation');
But is don't show any thing
This is because your config array is two dimensional array. So, you can't access directly email_validation without getting user first. Moreover,
$this->config->load('user'); just means loading user.php from application/config/ directory. Doesn't mean loading user index from $config array. You can do it like that.
$userConfig = $this->config->item('user');
echo $userConfig["email_validation"];
Edit
Please make sure you config file is under application/config/ and loaded.
$this->config->load('user');
You can check which config is loaded by doing like this.
echo "<pre>";
print_r($this->config);
echo "</pre>";
Hope it will be useful for you.
if your php version>=5.4 you can use this
$site_name = $this->config->item('user')['email_validation']

Store session in MemCache instead of default session storage in Yii 1.x

This is the code, that I've added to config/main.php in my Yii 1.x application:
'mCache' => array(
'class' => 'system.caching.CMemCache',
'useMemcached'=>true,
'keyPrefix'=>'',
'hashKey'=>false,
'serializer'=>false,
'servers' => array(
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 10000)
),
),
'session' => array(
'sessionName' => 'PHPSESSID',
'class' => 'CCacheHttpSession',
'autoStart' => true,
'cacheID' => 'mCache',
'cookieMode' => 'only',
'timeout' => 1200
),
What should I do next, to force Yii to use CMemCache, instead of default session storage?
I know this answer is old, but this configuration works
'memcacheConn'=>array(
'class'=>'CMemCache',
'servers'=>array(
array(
'host'=>'172.17.0.1',
'port'=>11211,
//'weight'=>60,
),
),
),
'session' => array(
'class' => 'CCacheHttpSession',
'autoStart' => true,
'cacheID' => 'memcacheConn',
'cookieMode' => 'allow',
'sessionName' => 'MYSSIONNAME',
),
Did you read introduction to CMemCache in Yii 1.x API documentation? I think you didn't. In first paragraphs of this document, you have an example, how to use CMemCache in Yii 1.x.
Change 'class'=>'CCacheHttpSession' into 'class'=>'CMemCache' in your session key of configuration file. And you don't have to register CMemCache as separate component, like you did in your example (mCache). You can configure it directly in session configuration key.
An example from Yii 1.x API documentation:
array
(
'components'=>array
(
'cache'=>array
(
'class'=>'CMemCache',
'servers'=>array
(
array
(
'host'=>'server1',
'port'=>11211,
'weight'=>60,
),
array
(
'host'=>'server2',
'port'=>11211,
'weight'=>40,
)
)
)
)
)

How to prevent files from being modified in elFinder?

I am using elFinder 2 + Codeigniter. And I would like to restrict users from deleting or modifying the existing files on all my folders.
I tried with this:
function elfinder_init(){
$this->load->helper('path');
$opts = array(
// 'debug' => true,
'roots' => array(
array(
'driver' => 'LocalFileSystem',
'path' => set_realpath('root'),
'URL' => base_url('root'),
//This didn't do the trick***
'defaults' => array('read' => true, 'write' => false, 'locked' => true),
)
)
);
$this->load->library('elfinder_lib', $opts);
}
It prevent users from uploading new files, but still allows them to modify/delete the existing ones.
Official documentation there is very vague in general and there is no info on how to achieve this, so if you could help me, I'll really appreciate it.
Extracted from their own GitHub issues tickets :
Here is an example to lock folder and subfolder write / delete
array(
'pattern' => '/.(lockedFolder1|lockedFolder2)/',
// Dont write or delete to this and all subfolders
'read' => true,
'write' => false,
'locked' => true
)
Here is an example to lock root but not subfolders :
array(
'pattern' => '/.(lockedFolder1|lockedFolder2)$/',
// Dont write or delete to this but subfolders and files
'read' => true,
'write' => false,
'locked' => true
)
Source

What is the Scope for using Google API Directory Services

I am already using a number of Google API services, such as Calendar and Google+ profiles, but using the Directory services is proving difficult.
Here is the scope I'm declaring in my local_config - everything has been working until I added the final line...
// Definition of service specific values like scopes, oauth token URLs, etc
'services' => array(
'analytics' => array('scope' => 'https://www.googleapis.com/auth/analytics.readonly'),
'calendar' => array(
'scope' => array(
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.readonly",
)
),
'books' => array('scope' => 'https://www.googleapis.com/auth/books'),
'latitude' => array(
'scope' => array(
'https://www.googleapis.com/auth/latitude.all.best',
'https://www.googleapis.com/auth/latitude.all.city',
)
),
'moderator' => array('scope' => 'https://www.googleapis.com/auth/moderator'),
'oauth2' => array(
'scope' => array(
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
)
),
'plus' => array('scope' => 'https://www.googleapis.com/auth/plus.login'),
'siteVerification' => array('scope' => 'https://www.googleapis.com/auth/siteverification'),
'tasks' => array('scope' => 'https://www.googleapis.com/auth/tasks'),
'urlshortener' => array('scope' => 'https://www.googleapis.com/auth/urlshortener'),
'directory' => array('scope' => 'https://www.googleapis.com/auth/admin')
I have tried a few different combos, but nothing seems to work - here is the error I'm getting...
Some requested scopes were invalid.
{valid=[https://www.googleapis.com/auth/tasks,
https://www.googleapis.com/auth/calendar, https://www.googleapis.com/auth/calendar.readonly, https://www.googleapis.com/auth/userinfo.profile,
https://www.googleapis.com/auth/userinfo.email], invalid=[https://www.googleapis.com/auth/admin]}
I'm trying to pull Group listings at the moment, but I'll need other Admin sdk features later.
Thanks! Let me know if I need to add any more details.
Admin SDK scopes are listed at:
https://developers.google.com/admin-sdk/directory/v1/guides/authorizing

ZF2 Doctrine2 Entity Cache

Does somebody know how to cache doctrine2 entities in a zf2 project. I cant find a tutorial or website where this is explained. I cant find any information to start with defining a entity filecache.
Somebody of you got working links or examples.
Thanks
You have two options
Use doctrine's native caching, e.g. using memcache (in the memcache block you can use any kind of doctrine supported cache, a full list of cache drivers is available).
Use doctrine's adapter for Zend/Cache/Storage to use another cache that you're using elsewhere; the adapter is described in the DoctrineModule docs.
As an example of version two, I have something like the following configuration in a module (actually spread across various config files, so I can't guarantee that copy-pasting verbatim will work).
'services' => array(
'factories' => array(
// Wraps a ZF2 cache storage in a Doctrine compatible way
'doctrine.cache.zend.static.local' => function ($services) {
return new ZendStorageCache($services->get('cache.static.local'));
},
),
'caches' => array(
// A ZF2 cache, can be configured as you like
'cache.static.local' => array(
'adapter' => 'xcache',
'plugins' => array(
'exception_handler' => array(
'throw_exceptions' => false,
),
'serializer',
),
),
),
'doctrine' => array(
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'zend.static.local',
'query_cache' => 'zend.static.local',
),
),
),
Note that Doctrine annoyingly automatically prefixes "doctrine.cache." to the name of the cache service that you configure, so while we configure "metadata_cache" to "zend.static.local", the actual cache service must be named "doctrine.cache.zend.static.local". Obviously you can call them what you want, but you'll need to add that prefix to whatever you call them.
To activate file cache you just need to add in your module.config.php
'doctrine' => array(
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'filesystem',
'query_cache' => 'filesystem',
)
),
)
and it will create cache automatically in data/DoctrineModule/cache folder
here is my full doctrine config for ZF 2.2.4 + Doctrine 2
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Modulename/Entity')
),
'orm_default' => array(
'drivers' => array(
'Modulename\Entity' => 'application_entities'
),
)
),
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'filesystem',
'query_cache' => 'filesystem',
)
),
),

Resources