简体   繁体   中英

Drupal preprocess function and session

I set a session var at template preprocess function in a theme I use, but the first time I open the site I cant read the session var, if I refresh the page it works fine, anybody know what can be the problem??

This is the code I use in the preprocess function:

function m_preprocess(&$vars, $hook) {
     $default_location = array(
        'country_code' => 'ec',
        'province_code' => 'p',
        'province' => 'Pichincha',
        'city' => 'Quito',
        'city_path' => 'lugares/u/ec/p/*'
     );
     if (isset($_COOKIE['proximity_path'])) $default_location['proximity_path'] = $_COOKIE['proximity_path'];
     $default_location['path'] = isset($_COOKIE['sort-by']) && $_COOKIE['sort-by']=='proximity'? $_COOKIE['proximity_path'] : $default_location['city_path'];
     $_SESSION['location'] = $default_location;
}

A couple of things:

  1. Try dsm($_SESSION); to see what is the var content when the site first load.

  2. I don't know where you create $_COOKIE['proximity_path'], but it is not in the code you show.

  3. Check for typos

The template pre-process function is called before node.tpl.php (that's why it is called pre-process) and that is why the value of the $_SESSION variable is not available in the template preprocess function.

Like you pointed out, it works after the first page load. This happens when only after $_SESSION variable is set in the node body (using the PHP filter)

我找不到解决方案,我认为要使用会话变量没有问题,您只需要在tpl.php文件中使用它即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM