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?
Do this instead:
if (isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == 1)
Use isset()
:
if(isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] == 1) {
// ...
}
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
}
You can use
if(isset($_SESSION['LoggedIn']))
{
//Shop stuff here
}
else
{
//Login stuff here
}
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
...or the politically incorrect answer if (@$_SESSION['LoggedId'] == 1) {...}
;)
精彩评论