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
https://forums.phpfreaks.com/topic/262127-issue-with-sessions/
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
https://forums.phpfreaks.com/topic/262127-issue-with-sessions/#findComment-1343362
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.