views:

58

answers:

3

Hi,

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: 

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

redhatlab
$_COOKIE['proximity_path'] is created in the client side, another thing to consider, I use the $_SESSION var into node content using php-code filter
juanca
This is the first value: Array ( [mobile-tools-mobile-device] => Array ( [type] => desktop [group] => ) [mobile-tools-site-type] => mobile ) and this is the value after refresh the page: Array ( [mobile-tools-mobile-device] => Array ( [type] => desktop [group] => ) [mobile-tools-site-type] => mobile [location] => Array ( [country_code] => ec [province_code] => p [province] => Pichincha [city] => Quito [city_path] => lugares/u/ec/p/* [path] => lugares/u/ec/p/* ) )
juanca
I test print_r($_SESSION) in the page.tpl.php and in the node content, the problem is only within node content
juanca
When you said you use the $_SESSION var into a node content using the PHP-code filter, do you mean that you initialize the $_SESSION var in the node content or that you use content of the $_SESSION var in your node content? Also based on the output of the $_SESSION var the preprocess function is not been loaded the first time. Do you have this code in the phptemplate_preprocess() function?
redhatlab
I create the $_SESSION['location'] var in preprocess function and I read it into node content, I test with session_start() and without it but it doesnt exists the first time
juanca
Just for testing add {} to if (isset($_COOKIE['proximity_path'])) $default_location['proximity_path'] = $_COOKIE['proximity_path'];
redhatlab
I set any value to $_SESSION['location'] var like a string, but it behavior is the same, consider that problem only happens in node content, if I read the var in page.tpl.php works fine
juanca
That's odd. Something simple might be blocking it, that is why I thought of the IF. Are you putting this code in phptemplate_preprocess()?
redhatlab
I think that preprocess function is called after node php-code is process?? do you know how call a function before node php-code has been processed??
juanca
I use m_preprocess function where "m" is the theme name
juanca
redhatlab
nop, I suspect that php-code with in node body is processed before theme tpl.php files, it explain why the problem is only the first time, the problem I think is how to call a function before node content is processed
juanca
A: 

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)

Sid NoParrots
what do you recommend to do??
juanca
A: 

I could not find a solution, I think to use session vars without problems you need to use it in tpl.php files only.

juanca