session_unset runs even if it is not supposed to.

Posted on 2006-04-10
Medium Priority
Last Modified: 2010-05-18
I have a script with an if... else... statement.

I want to session_unset only if the else statement runs, but for some reason it runs no matter where it is located in the code.

Has anyone else experienced this problem?
Question by:vertex_paul
  • 2
LVL 15

Expert Comment

ID: 16422368
It sounds like you just have a problem with how your code is structured.  If the unset is inside your else statement, it's going to get executed in every instance where the "if" part of the statement is not met.  Example:

if ($variable == "test") {
    // test code
} else {
    // unset code

Every time $variable is not equal to "test", your else code is going to be executed, including the unset.  Now, if you are passing a valid value that should only excute the top part of code (in this example, $variable would have to equal "test") and the "else" part of the statement is still being excuted, then you need to investigate the variables that are used in the "if" line of the statement.

Expert Comment

ID: 16423225
you may want to use exactly equal (===) instead of just equal (==) because automatic type conversion may be causing problems

Author Comment

ID: 16426387
The code is structured correctly, it is written exactly like the code in the comment from Tomeeboy, and the proper code is running.  The test code from the if statement is running when it is supposed to, but if I place the unset code in the else statement it also runs, but none of the other code in the else statement runs.  This is why it is so confusing.  Could it be a bug in php?

Accepted Solution

quad341 earned 250 total points
ID: 16426417
from your desciption, it sounds like it. what version of php are you running?

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question