0

I have a webapp that uses login/logouts so I have session management. Basically every page so far starts with

session_start();  
if(!isset($_SESSION['username'])) {header("Location: index.php");} else { rest of the page's functionality}

I am now creating a class (User.php); that will be accessed by another .php page. Do I need to implement the above for security, and if so how? Should I put classes above the webroot? Thanks

1 Answer 1

1

first of all, although i assume you've thought of this; just to check if the username is set in a session is not particularly save. If you want to check if a user is logged in some additional tests should be present.

than back to your question; a page could possibly access the User class even if no logged in user exists (eg. when you want to display this particular users' public comments on a blog post). So no, your test would not be needed. Furthermore you could also build in a check if the user is logged in into the User class (or better still; the Authentication class you'll build around it), so you could do something like:

if(Authentication::is_logged_in($_SESSION['username']) === true) {
    echo 'yeeehaaaa! You\'re logged in bro!';
} else {
    echo 'what are you doing here?! Get lost! (or log in)';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, what other security measures should I take to make sure a user is logged in? How can someone get past the if(!isset($_Session['username'])) security?
you might want to store a timestamp to check the expiry (eg. 1 hour after login), but definitely a password hash build up from eg. the timestamp, (encrypted) password, a constant and the clients public ip. This will prevent someone from 'stealing' the session. The hash is created at login in and checked every time the hypothetical is_logged_in() function is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.