Jump to content

Keep user logged in


manix

Recommended Posts

Hey,

 

I came across this problem where it's all good when a user logs in and it's all cool and stuff but then when he's hyperlinked to a page or something and he has to log in again. Now I went through a couple of tutorials and all I understood was that I have to use session_start function, and I could just copy/paste the code but I want to really get it, the way it works. Could someone bother explaining this to me?

 

Long story short - how do I maintain the user logged in through all my pages ?

 

Thank you for your time.

Link to comment
Share on other sites

In simple terms, by adding session_start(); at the top of each page, you have access to using the $_SESSION variable. These variables will exist until:

 

a) your game/application/page destroys them (with session_destroy() )

or

b) until browser is closed.

or

c) until the session time limit in your main configuration is reached

 

so imagine you simply add session_start() to the top of all your pages (including the page where you have your login script)

during a successful login, at the end write:

 

$_SESSION['loggedIn'] = 1; //(or whatever names/values you want)

 

then on each page, just check that variable

 

<?php
session_start();
if( !isset($_SESSION['loggedIn']) || $_SESSION['loggedIn'] != 1 ){
   // user is lot loged in, redirect, show message, etc...
   exit();
}

 

Link to comment
Share on other sites

c) until the session time limit in your main configuration is reached

 

Can't I set the expiration time ?

I mean can I force the session to last longer or do I always have to start the session in all my documents

Link to comment
Share on other sites

$_SESSION is used for many other reasons, not just for logins. it's useful to store data while processing, to move data from page to page, etc.. But yes, a cookie (as long as the user has the browser set to remember cookies) will store you login info for longer (depending on the cookie timeout you define).

 

I, personally, do not like cookies. Not only because they depend on browser settings that I  cannot control but also because I consider them a security issue, mainly because you cannot trust users to protect their data.

 

I work at a financial servicing company, and out of our 400 employees, about 250 will never remember to lock their computer when they leave... See how serious a cookie can become? Anyone passing by will have access to their accounts, intranet, email, possibly other passwords stored in email...

 

Of course a session has the same issues, but if you control the timeout, you're only vulnerable for about 15 minutes (that's the inactivity time I normally set).

 

Also, with sessions, a text file is created on your server, so you can easily kill any user's session (force logout), whenever you want just by deleting the correct session file.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.