Reputation: 79
I have a link in page A that directs to page B. page B has a function like this:
function s(){
$_SESSION['active']='0';
}
s();
echo $_SESSION['active'];
The problem is that in order for function s() to be called and $_SESSION['active']
be set, I need a second refresh, and this adds some overhead. I don't know why this function is not called when directing from page A to B? any help would be appreciated.
EDIT 1: I have session_start(); at the very top of every php script. the only solution I could come up with is a second refresh
EDIT 2: I have inspected the code very carefully, echoing every variable in my function and also using XDEBUG. the problem is as I stated, when directing to page B the function is not called until I refresh the page.
EDIT 3: Problem solved. it was a very stupid mistake, that session variable was unset somewhere in the code preceding that function. that problematic function was part of nested functions, and I am using circular references in number of functions which makes my code hard to read, That is why I could't post here. Thanks ever one, it is really a shame.
Upvotes: 0
Views: 63
Reputation: 3832
There are a couple of choices. One could move the session code to page A so that when it redirects to B, one could see the updated session value. Or, you could try something like this on page B:
<?php
session_start();
function s(){
$_SESSION['active']='0';
}
s();
session_write_close()
session_start();
echo $_SESSION['active'];
See demo.
You may wish to review a related discussion here.
Note, however, that when I ran this code:
<?php
session_start();
function s(){
$_SESSION['active']='0';
}
s();
echo $_SESSION['active'];
at 3v4l.org it ran fine with PHP 5.2.3-5.6.30 and 7.0.0-7.2.0beta1. If one is using PHP 4 or an earlier version of PHP 5, the function does execute, but the result comes with warnings.
Upvotes: 1
Reputation: 27503
function s(){
$_SESSION['active']='0';
return $_SESSION['active'];
}
$result =s();
echo $result;
Try this as there is no return from the function
Upvotes: 1