1

I've been searching all day and found several similar issues, but none have resolved my problem. As the title states I'm having trouble with session variables not being "saved" - i.e they work only like local variables. What I'm trying to accomplish:

index page with function "ABC" not running by defualt - link to myscript.php to activate session variable of type boolean - myscript.php checks if session variable is set, if not turns it to true. Otherwise turns it to false: index page should now have function "ABC" activated.

So, here is the funny thing. This worked like a charm earlier today, but after a random refresh, now it don't. Thinking this was a session trouble, I added a session ID but found that the session ID is correct / the same on both the index page and myscript.php.

I've also tried to enable error reporting and found that I'm getting the "Undefined index: showAll in C:\xampp\htdocs\kelvin\ext\set_date.php on line 15". Which is weired, seing as i use the issset function to avoid just that.

myscript.php

<?php
session_start();
echo "Session ID: " . session_id(); //Is the same as on the index page where the script is being called.
---------------
//Sets speed mode on or off (simple or extensive listing).
if(!isset($_SESSION['showAll']))
{    
    $_SESSION['showAll'] == TRUE;
}
 else {
    if($_SESSION['showAll'] == TRUE)
    {
        $_SESSION['showAll'] = FALSE;
    }
    else
    {
        $_SESSION['showAll'] = TRUE;
    }
}
header('location:../index.php');
?>

In advance, thanks for any input :)

1
  • have you turned session on in the other file? Commented Jun 4, 2011 at 17:39

1 Answer 1

5

Your problem might be here:

if (!isset($_SESSION['showAll'])) {
  $_SESSION['showAll'] == TRUE;
}

The double equal sign (==) checks for equality, but does not set anything.

Try replacing it with the single equal sign (=).


Also, your logic can be condensed considerably:

if (!isset($_SESSION['showAll'])) {    
  $_SESSION['showAll'] = true;
} else {
  $_SESSION['showAll'] = !$_SESSION['showAll'];
}

You are just swapping the value of $_SESSION['showAll'].

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Turns this happend because I copyed from an old template with the double equal sign error. Everything was the same from the template expect the small typo error. Regarding the logic I disagree though, or at least, I don't see it working that way. The idea is it's a function that can be turned on or off via the HTML link.
The logic is simple. In yours, you first check if var is true. If it is, you set it to false. If not, you set true. That swaps true with false and false with true. Mine does the same, as all I do is set var to its opposite.
Aha, I was not aware of that. Thanks again! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.