Problem disconnecting session variable

My site has a form used to invite friends. This is a simple text box and a submit button. If there is an error, I redirect back to this page and post an error message if they have a set of sessions.

if (isset($_SESSION['invite_error'])) { echo $_SESSION['invite_error']; unset($_SESSION['invite_error']); } 

However, if I go back to this page and return to it, the error message is still displayed. If I leave and return again, it will be done. The same thing when I update this page ... 1 update will not get rid of it, but 2 will. I can’t destroy the whole session, I just want to disable this variable. The PHP version is 5.2.5 build 6, the registration of global variables is disabled, I call session_start () at the top of this page, I also tried to use the header without a cache.

Edit: Added full code.

 <?php ob_start(); session_start(); $user_id = $_SESSION['user_id']; $user_name = $_SESSION['user_name']; if ($user_id==null) header("Location: /login.php"); if (isset($_SESSION['invite_errors'])) { $error = $_SESSION['invite_errors']; unset($_SESSION['invite_errors']); } require_once("ui/header.php"); ?> <div id="invite" class="content"> <?php if($error) { ?> <div class="errors round"> <?php echo $error ?> </div> <?php } ?> <h3>Invite Your Friends</h3> <div class="invite-form"> <form method="post" action="controllers/invite.php"> <div class="row"> <textarea class="txt-area" name="emails" id="emails" rows="5"></textarea> <div class="tip">Separate multiple email addresses with ,</div> </div> <div class="row-submit"> <input type="submit" name="submit" id="submit" class="submit-btn" value="Submit" /> </div> </form> </div> </div> <?php require_once("ui/footer.php"); ?> 
+6
php session session-variables
source share
4 answers

Start a session before starting output buffering. So switch to the ob_start() and session_start() calls.

Since session cookies are defined in headers sent to the browser, and headers are sent to the browser when the buffer starts, you must start sessions before the buffer.

+13
source share

The example you provided should work. Perhaps some session cache is bothering you. You can try changing the code as follows:

 if (isset($_SESSION['invite_errors']) && $_SESSION['invite_errors']) { $error = $_SESSION['invite_errors']; $_SESSION['invide_errors'] = false; unset($_SESSION['invite_errors']); // Explicitly write and close the session for good measure session_write_close(); } 
+2
source share

Perhaps you can change the value after the error echo

 if (isset($_SESSION['invite_error'])) { echo $_SESSION['invite_error']; $_SESSION['invite_error'] = null; unset($_SESSION['invite_error']); } 
+1
source share

In my experience, PHP sessions are not worth using and were designed only as a quick and easy solution. The behavior of $ _SESSION is not always obvious, but using sessions in the traditional sense is mostly inevitable. Issue your own id and token cookies and save the session values ​​against them, possibly in a database. It can also be scalable on different servers.

+1
source share

All Articles