Page 1 of 1
Most efficient way to check if user is logged in and then...
Posted: Thu Nov 17, 2011 2:05 pm
by Markto
What is the most efficient way to check if user is logged in and then display different information for each state? I am currently using an if/else statement, but this seems a bit messy, because there is a ton of code under the else.
Example:
Code: Select all
if(isset($_SESSION['idmember']))
{
//the pages code.
} else {
echo "you must be logged in";
}
as you can probably tell, this can lead to a very long if statement, and it can get a bit confusing. I was wondering if there was a better way to check if a user is logged in without including all of my code in an if else statement.
Re: Most efficient way to check if user is logged in and the
Posted: Thu Nov 17, 2011 2:06 pm
by Celauran
You could always just reverse the order...
Code: Select all
if (!isset($_SESSION['whatever']))
{
// They need to login
}
Re: Most efficient way to check if user is logged in and the
Posted: Thu Nov 17, 2011 2:42 pm
by Markto
But that would still nest all of my code in this big "if" statement. I was hoping there was some more elegant way of solving this problem, like checking to see if the user is logged in, and if not, just quitting (exit() maybe?), and then having all of the code follow this if statement, which I could include at the top of every relevant page.
Re: Most efficient way to check if user is logged in and the
Posted: Thu Nov 17, 2011 2:44 pm
by Celauran
Markto wrote:But that would still nest all of my code in this big "if" statement.
How so? The existing if redirects them if they aren't logged in. No need for any additional conditionals.
Re: Most efficient way to check if user is logged in and the
Posted: Thu Nov 17, 2011 3:09 pm
by Markto
I think you are right. My mistake!

I misinterpreted your code. Thanks for going easy on me - I appreciate the help.