开发者

Zend cookie issue

开发者 https://www.devze.com 2023-04-12 15:22 出处:网络
I\'m working on a Zend 1.11 webapp. I built a language-selector plugin for switching language (it & en) depending on the user input.

I'm working on a Zend 1.11 webapp. I built a language-selector plugin for switching language (it & en) depending on the user input.

This is what I have:

class LS_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract
{

public function preDispatch(Zend_Controller_Request_Abstract $request)
{


    if($request->getParam('lang'))
    {
        $lang=$request->getParam('lang');
        setcookie('lang', $lang, time() + (3600));
        echo "we just set a cookie";    
    }
    else if (isset($_COOKIE['lang']))
    {
        $lang=$_COOKIE['lang']; 
        echo $lang;
    }
    else 
    {   echo 'We are here.But I can't understand why';
        $lang='en';
        var_dump($_COOKIE);
    }

    switch(strtolower($lang))
    {
        case 'en':
            $locale="en_US";
        break;

        case 'it':
            $locale="it_IT";
        break;

        default:
            $locale="en_US";
    }

    $zl=new Zend_Locale();
    $zl->setLocale($locale);

    Zend_Registry::set('Zend_Locale',$zl);



    $translate=new Zend_Translate(
                  array(
                          'adapter' => 'gettext',
                          'content' => APPLICATION_PATH.'/configs/languages/'.$locale.'.mo',
                          'locale'  => 'en'
                      )

    );

    Zend_Registry::set('Zend_Translate',$translate);
}   

}

[bootstrap.php]

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

protected function _initRoutes()
{

    $frontController=Zend_Controller_Front::getInstance();
    $router=$frontController->getRouter();  
    $router->removeDefaultRoutes();
    $router->setGlobalParam('lang','en');
    $router->addRoute(
            'lang',
            new Zend_Controller_Router_Route('/:lang/:controller/:action',
            array('lang'=>':lang',
            'module'=>'default',
            'controller'=>'index',
            'action'=>'index'

            )

            )
    );


    $router->addRoute(
    'langController',
    new Zend_Controller_Router_Route('/:controller/:action',
    array(
            'module'=>'default',
            'controller'=>'index',
            'action'=>'index'
            )

    )
);

            $router->addRoute(
    'langIndex',
    new Zend_Controller_Router_Route('/:lang',
    array('lang'=>':lang',
            'module'=>'default',
            'controller'=>'index',
            'action'=>'index'
            )

    )
);

            $router->addRoute(
    'langNothing',
    new Zend_Controller_Router_Route('',
    array('lang'=>'en',
            'module'=>'default',
            'controller'=>'index',
            'action'=>'index'
            )

    )
);

}
}

The point is that:

  • I type in my browser the full URL mysite.com/it/index/index and I get echo out "we just set a cookie"..wich is fine I should be able to browse my website with the italian cookie set but I don't. Whenever I click over a link (Ex. /index/contactus.开发者_C百科.links do not specify the 'lang' parameter!) I navigate to that page but it's in english again (I get echoed out the message:"we are here.but I can't understand why").

Shouldn't the cookie be set?


The problem is, that you don't specify the path for your cookie. So it will set for the current page only.

If you browse to another (sub)site, then your cookie is not valid for this path and doesnt get transmitted. You should set your lang-cookie to the "/" Path.

See setcookie() documentation:

The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号