0


i'm trying to check if a session exist and has a specific value (2) to show a part of the website and if not it should return you to a other page.

So far i got this:

$_SESSION["deel"] = "2";  //set session deel to 2
$result = array("error" => false, "html" => null);
$result["error"] = false;
$result["html"] = "<h3>Session information:";
$result["html"] .= "<a href='/shop/?,69'>$_SESSION[class2C]</a>";
$result["html"] .= "</h3>";

after that we go to a other page:

if (!isset($_SESSION['deel']) || $_SESSION['deel'] == '2')
{ show the shit }
else 
{ redirect }

However, it doesn't do anything and just shows me "the shit"
what am i doing wrong here?

Thanks in advance.

Justin
  • 49
  • 1
  • 1
  • 8
  • 2
    How about a session_start() before you use session? – Rizier123 Nov 27 '14 at 16:17
  • this is only a part of the total script and yes the session gets started. in above scripts and this file itself. – Justin Nov 27 '14 at 16:27
  • 1
    Then why you check if the session['deel'] is NOT set OR it's equal to 2? So if it's not set you show sh*t, but if it is set and equal's to 2 you also show sh*t – Rizier123 Nov 27 '14 at 16:28

1 Answers1

2

Your condition is wrong

if (!isset($_SESSION['deel']) || $_SESSION['deel'] == '2')
    ^                         ^^

Means if the session is not set or if it is set then the value is 2, This will be true even if there is no session. You should do:

if (isset($_SESSION['deel']) && $_SESSION['deel'] == 2)

This can also be derived from your own statement

i'm trying to check if a session exist and has a specific value (2)

                      ^       ^          ^
 if (isset($_SESSION['deel']) &&  $_SESSION['deel'] == 2)
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • damnit, im still confusing the !isset and isset.. didnt know what the || did in this case i thought i was something similar to && – Justin Nov 27 '14 at 16:48
  • Oops then you must go through this post http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Hanky Panky Nov 27 '14 at 16:49
  • `!` before something means `NOT` of that. `&&` means `AND`, `||` means `OR`. That post i mentioned explains it all clearly – Hanky Panky Nov 27 '14 at 16:50
  • Thanks again dude. Didn't check it out yet but i do now and i found it very usefull :) – Justin Jan 28 '15 at 15:16