Zend Framework 2 如何更改 sessionid 值 [英] How to change sessionid value Zend Framework 2

查看:25
本文介绍了Zend Framework 2 如何更改 sessionid 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 PHPSESSID 名称和值.我可以命名,但我不能改变价值.我有以下结构.我如何更改 sessionid 值.

i want to change PHPSESSID name and value. I can could name but i couldn't change value. i have following stracture. How i change sessionid value.

我的 module.config.php 是

My module.config.php is

return array(
    'session'         => array(
        'config'     => array(
            'class'   => 'Zend\Session\Config\SessionConfig',
            'options' => array(
                'name' => 'portal1'
            ),
        ),
        'storage'    => 'Zend\Session\Storage\SessionArrayStorage',
        'validators' => array(
            array(
                'Zend\Session\Validator\RemoteAddr',
                'Zend\Session\Validator\HttpUserAgent',
            ),
        ),
    ),
);

我的模块.php

public function onBootstrap($e) {
    $this->bootstrapSession($e);
}

public function bootstrapSession($e) {
    $session = $e->getApplication()
            ->getServiceManager()
            ->get('Zend\Session\SessionManager');
    $session->start();

    $container = new Container('initialized');
    if (!isset($container->init)) {
        $session->regenerateId(true);
        $container->init = 1;
    }
}

public function getServiceConfig() {
    return array(
        'factories'  => array(
            'Zend\Session\SessionManager'           => function ($sm) {
                $config = $sm->get('config');
                if (isset($config['session'])) {
                    $session = $config['session'];

                    $sessionConfig = null;
                    if (isset($session['config'])) {
                        $class         = isset($session['config']['class']) ? $session['config']['class']
                            : 'Zend\Session\Config\SessionConfig';
                        $options       =
                            isset($session['config']['options']) ? $session['config']['options'] : array();
                        $sessionConfig = new $class();
                        $sessionConfig->setOptions($options);
                    }

                    $sessionStorage = null;
                    if (isset($session['storage'])) {
                        $class          = $session['storage'];
                        $sessionStorage = new $class();
                    }

                    $sessionSaveHandler = null;
                    if (isset($session['save_handler'])) {
                        $sessionSaveHandler = $sm->get($session['save_handler']);
                    }

                    $sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);

                    if (isset($session['validator'])) {
                        $chain = $sessionManager->getValidatorChain();
                        foreach ($session['validator'] as $validator) {
                            $validator = new $validator();
                            $chain->attach('session.validate', array($validator, 'isValid'));

                        }
                    }
                } else {
                    $sessionManager = new SessionManager();
                }
                Container::setDefaultManager($sessionManager);

                return $sessionManager;
            },
        ),
    );
}

MyController.php 是;我想在这里更改 PHPSESSID 键和值.

MyController.php is; i want to change PHPSESSID key and value here.

public function loginAction() {
    $container = new Container();    /*
    i want to change PHPSESSID key and value
    eg: portal1: fafsafg43kgfdsgfds //my sessionid value
    */
}

推荐答案

好吧,我想在回答问题之前澄清一些事情 -什么意思我想更改 PHPSESSID 键和值"据我了解,您想更改PHPSESSID"文本.这意味着你想用不同的名字来调用它,比如MySessionId",不是吗?

Well, I want to clarify something before answer the question - What dose it means "i want to change PHPSESSID key and value" As far I understand you want to change the "PHPSESSID" text. that means you want to call it in different name like "MySessionId" isn't it?

实际上我无权在我可以提问的地方写评论!

Actually I'm not authorized to write comment where I can ask!

无论如何,如果你想用不同的名字来调用它而不是 ZF 的问题,它是你服务器的 PHP 会话设置.因此,请咨询服务器的会话部分.您可以从 http://php.net/manual/获得帮助en/session.configuration.php#ini.session.namehttp://php.net/manual/en/function.session-name.phpZF 还允许您通过 Zend\Session\Config\SessionConfig 类或 Zend\Session\SessionManager 类更改该名称,您必须通过调用 setName 方法设置首选名称.

Anyway, if you want to call it with different name than its not the issue of ZF its your server's PHP session settings. So, consult the session part of the server. You may get help from http://php.net/manual/en/session.configuration.php#ini.session.name OR http://php.net/manual/en/function.session-name.php And ZF also let you change that name by Zend\Session\Config\SessionConfig class or Zend\Session\SessionManager class you have to set the preferred name by calling setName method.

现在,如果您想更改会话 ID 的值(自动生成并保存到 PHPSESSID),您可以在 ZF 中进行.这是ZF2显式设置值的代码

Now if you want to change the value of session id (its automatically generated and save it into PHPSESSID) you may do it in ZF. Here is the code of ZF2 to set the value explicitly

$container->getManager()->setId($id); # For current session manager of your container
$container->getDefaultManager()->setId($id); # For default session manager of your entire app

在上面的代码中,$id 是您要设置的值.

In above code $id is the value you want to set.

这篇关于Zend Framework 2 如何更改 sessionid 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆