How do you deal with logins?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

How do you deal with logins?

Post by JellyFish »

Hey, I was wondering how devneters usually go about dealing with session logins, just because I'd like to get an idea of all the different ways to do it. I know you store something in the $_SESSION variable, but what? The way I was thinking of doing it was this:

Code: Select all

 
$_SESSION['log'] = array(
  "email" => $email,
  "password" => $password,
  ...
);
 
and to check if a user is logged in:

Code: Select all

 
if ($_SESSION['log'])
{
//user is logged in
}
 
But now I'm tied up on what to call this array. Should I call it 'log' or should I call it 'login', 'logged', or 'user_log'? :? :lol: Does it really matter? I guess not but I want something that makes sense. What do you do?

Thanks for reading.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you deal with logins?

Post by requinix »

It doesn't matter. And you can put whatever you want into $_SESSION.

You don't need a variable just to keep track if the user is logged in. Create a convention: if there is an [email] then the user logged in successfully, otherwise they did not.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: How do you deal with logins?

Post by JellyFish »

tasairis wrote:It doesn't matter. And you can put whatever you want into $_SESSION.

You don't need a variable just to keep track if the user is logged in. Create a convention: if there is an [email] then the user logged in successfully, otherwise they did not.
I'm not so sure I know what you mean by convention. Do you mean check if $_SESSION['email'] is set instead of $_SESSION['login']?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you deal with logins?

Post by requinix »

It means you need to decide upon something and keep it standard across the website. Decide that (for example) $_SESSION["email"] will exist if and only if the user has logged in successfully.

The stuff with email versus login is a side-effect of what such a policy can do. Since you can use the email to check if a user logged in or not, you don't need an extra login variable.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: How do you deal with logins?

Post by Griven »

If you're looking for ideas of what to call it, then you're likely going to get different answers depending on who you talk to. However, I think that most of us can agree on this: whatever you decide to call your variables, it should be something you'll be able to recognize six months down the road when you're revisiting this code.

For example, if you call your variable $_SESSION['log'], you may wonder what it means in the future. Were you talking about an event log? A log file? A block of wood?

Try to use a name that is self explanatory and points to the purpose of the variable, such as $_SESSION['loggedin'], ['email'], etc, etc.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: How do you deal with logins?

Post by JellyFish »

Okay, I'm going with $_SESSION['login']. Thanks for all the support. :)
Post Reply