views:

60

answers:

6

Hi guys, I'm making an item shop for a game of my friends, when accessing the shop, I have it check the session to see if you are logged in, if you are it will take you to the shop, if you aren't it will give you a login page, the way I do that is like this.

<?php 
  if($_SESSION['LoggedIn'] == 1)
  {
      //Shop stuff here
  }
  else
  {
    //Login stuff here
  }
?>

However, it shows me an error when they aren't logged in.

Notice: Undefined index: LoggedIn in C:\wamp\www\shop\shop.php on line 29, line 29 being the if($_SESSION['LoggedIn'] == 1) I want to stop this from happening without disabling the PHP errors, any idea how?

+2  A: 

Use isset():

if(isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == 1) {
  // ...
}
halfdan
+3  A: 

Do this instead:

if (isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == 1)
Delan Azabani
A: 

You can use

if(isset($_SESSION['LoggedIn']))
{
    //Shop stuff here
}
else
{
    //Login stuff here
}
nik
+1  A: 

To add some variety to the answers, I'd like to give you empty :

if(!empty($_SESSION['LoggedIn'])){
    //Shop stuff here
}
else{
    //Login stuff here
}
Arkh
A: 

to use $_SESSION it is important to start the session first with the instruction session_start ();

session_start();

if($_SESSION['LoggedIn'] == 1)
{
  //Shop stuff here
}
else
{
//Login stuff here
}

if this statement is not present the session will not be open, that means that every informations that you have already put there will be inaccessible, so be careful while reading or writing in $_SESSION

Leonzo Constantini
A: 

...or the politically incorrect answer if (@$_SESSION['LoggedId'] == 1) {...} ;)

Amati
-1 for advising bad practice.
Jacco
Care to explain why is it a bad practice? People often cry "bad practice!" when they see '@', without even stopping to think. Can you give me an example when the '@' above will suppress a vital error thus hiding some important information from you? For me '@' is a handy way to suppress notices and the way I used it above is precisely equivalent to the `isset()` examples. Now, if I used `@fopen('/some/file')` that would be a totally different story.
Amati
Using error suppression is almost always a sign of laziness. If you choose to suppress an error instead of doing proper checking, that is bad practice. The only time a programmer should ever use error suppression is when proper error checking is done after the function that triggers the error (for example through return type checking. Actually, your example of `@fopen(...);` is one of those cases. (`if (($fp = @fopen($filename, 'r') === FALSE) { handleError(); }`).
Jacco
This is not an answer. I'm not trying to discuss if error suppression is a sign of laziness or not, in general. I was asking what is the difference between isset() and @ in the case at hand. Is there any scenario you can come up where isset() will produce different result than error suppression? Is there any difference in *this* case or is it "bad practice" just because it's included in some bullet list of "bad practices in php"?
Amati
First, I never repeat things 'because they are on a list'. I think I already answered your question. Suppressing the error, in this particular example, is a sign of laziness. When you do the explicit isset testing, the next programmer (or you, after a year or so) can read a bit of extra information (this variable might not be set at this point in the code, etc.). If you suppress the error, the next programmer has to guess: was this error suppressed because of a quick bugfix? was it part of the intended program flow? did the original programmer know why the suppressed error popped up?, etc.
Jacco
Then, this is quite obviously a beginners question. You just handed somebody a dangerous tool that, more often than not, is misused in PHP. If you had added an explanation as to why one could use it in this particular case, but should be very cautious of error suppression in general, I would not have downvoted you. But you just dropped a single line, without any explanation. Now, the OP could think: "Hey, that's handy, whenever there is an error that has no meaning to me, I can just turn it off!'.
Jacco