session_unset runs even if it is not supposed to.

Posted on 2006-04-10
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
    LVL 15

    Expert Comment

    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.
    LVL 6

    Expert Comment

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

    Author Comment

    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?
    LVL 6

    Accepted Solution

    from your desciption, it sounds like it. what version of php are you running?

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now