Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3672324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:35:40+00:00 2026-05-19T02:35:40+00:00

I guess I am not understanding the scope of session variables, or the session

  • 0

I guess I am not understanding the scope of session variables, or the session itself, in PHP, hence this question:

This is my code

  if(session_id()!=""){
  echo "Getting rid of session"."</br>";
  session_destroy();
 }
 echo "Before session_start(): ".isset($_SESSION["first_date_of_week"])."</br>";
 session_start();
 echo "After session_start(): ".isset($_SESSION["first_date_of_week"])." ".$_SESSION["first_date_of_week"]->format("Y-m-d")."</br>";

The output is:

Before session_start():
After session_start(): 1 2011-01-09

How come that when doing the isset(..) on the session variable it is set directly after starting the session, even though I haven’t even used it or set it yet? It does, however, still have the same value as before.

Also, session_id()=”” since the if-clause is never triggered. I never kill the session, how come it is set to “”? I.e. I refresh the page and expects the session to still be alive.

Using the isset(..) function is then pretty useless testing if it has been set already…

Thanks in advance!

/Niklas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:35:41+00:00Added an answer on May 19, 2026 at 2:35 am

    There are a couple of problems with the code:

    1. The test !session_id()=="" is wrong because ! has a higher precedence than ==. It should be written as session_id() != "". Due to implicit conversions it should work correctly, but I can’t say it’s anything else than a bug.
    2. session_destroy only works if called after session_start.

    Now, here’s what happens exactly:

    1. You test for an active session. Since there’s none, the test fails and session_destroy is never called (it would do nothing even if it were called).
    2. You test for the existence of $_SESSION["first_date_of_week"]. Since there’s no active session, isset return false.
    3. You start the session. $_SESSION["first_date_of_week"] now becomes available, but only because you had set it on some earlier session, in code that you don’t show.

    Try this code to get a better handle on what’s going on:

    $logout = true; // play with this
    session_start();
    if ($logout) {
        session_destroy();
        echo "Logged out.";
        die;
    }
    
    if (!isset($_SESSION['counter'])) {
        $_SESSION['counter'] = 1;
    }
    else {
        ++$_SESSION['counter'];
    }
    
    echo "The counter value is ".$_SESSION['counter'];
    die;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I guess I'm not understanding something about C++: I have this code: #include window.h
Based on example found here but I guess I'm not understanding it. This works
This is a question about best practices i guess but it applies directly to
I guess my question is in understanding the $_POST handling. So I have two
I'm not exactly sure whether or not this is a silly question, but I
I guess I'm not understanding the fundamentals of the PayPal sandbox. I've created a
I have a variation of the benefits-of-async/await-on-ASP.NET from this question . My understanding is
I'm trying to do something here with VB that I guess I'm not understanding
I thought I understood sed but I guess not. I have the following two
[Edit] And, I should add, most elegantly? I guess (but not sure how) I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.