I have a problem with sessions in wordpress. First I've activated the use of sessions in my functions.php and no I get the right variable but just after I reload my target site after setting the variable. When I load another page first and my target page seacondly my variable has a wrong value without any context to my first value. When I have the right value on my target site and I reload this site the value is also wrong. This is my code:
//Activate sessions() in functions.php add_action('init', function(){ if(!session_id()) { session_start(); } }, 1); //Calculate the variable $gesamtumsatz_kcal = round (($value1 + $value2 + $value3), 0); //Then set the session variable $_SESSION['gesamtumsatz_kcal'] = $gesamtumsatz_kcal; //Get my session variable on another page somewhere on my site if(isset($_SESSION['gesamtumsatz_kcal'])) { $gesamtumsatz_kcal = $_SESSION['gesamtumsatz_kcal']; } else { $gesamtumsatz_kcal = ''; } //Echo my variable echo $gesamtumsatz_kcal;
Do you have any idea whats wrong? I'm absolutely at the end with this....
Thanks a lot!
When I define a constant variable without calculate something like this:
//Define the variable $_SESSION['test'] = 1056; //Get the variable $test = $_SESSION['test']; //Echo the variable echo $test;
I always get the right value: 1056 back... What is this? Can't uderstand
wordpres destroys session when it is not opened. so use
if(!session_id()) session_start();
in the start of wp-config.php
or better in the init
of your code/plugin
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.