Jump to content

Issue with SESSIONS


dweb

Recommended Posts

Hello

 

Wonder if you can help;

 

I have an issue with sessions, I have stripped out a ton of code to give a more simple example below;

 

Basically when I click the "Button 1" link, it should just display "Clicked button 1" and when I click the "Button 2" link, it should just display "Clicked button 2"

 

But for some reason it's displaying "Clicked button 1" and "Clicked button 2" no matter what

 

Any idea why?

 

Thank you

 

<?php
session_start();
if(isset($_GET['1'])) { $_SESSION['result'] = 1; }
if(isset($_GET['2'])) { $_SESSION['result'] = 2; }
?>
<?php if(isset($_SESSION['result']) == 1) { echo "Clicked button 1<br>"; } ?>
<a href="?1">Button 1</a>
<br />
<br />
<?php if(isset($_SESSION['result']) == 2) { echo "Clicked button 2<br>"; } ?>
<a href="?2">Button 2</a>

<?php
unset($_SESSION['result']);
?>

Link to comment
Share on other sites

if(isset($_SESSION['result']) == 1)

 

isset returns true or false indicating if the variable is set. Since it is set (in the code above that), it is returning true. When you compare true to any integer (1 or 2) the IF is returning true. So, both IF statements, as written, are true.

 

if ( (isset($_SESSION['result'])) and ($_SESSION['result'] == 1) )

should produce the results you are expecting.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.