Lance O. Posted February 3, 2017 Share Posted February 3, 2017 I have a custom login page where a user should be directed to a members only page when the form is submitted and the user has been validated. What is wrong with my logic in the code below? Regardless of the user's role, the user is never authenticated and displays as a guest. if ( $input->post->user || $input->post->pass ) { // user submitted the login form if ( $session->login($input->post->user, $input->post->pass) ) { // user was authenticated and logged in // user has "admin" or "superuser" role foreach ($user->roles as $role) { $content .= $role->name . "<br/>"; } } else { // user is not authenticated // user is "guest" foreach ($user->roles as $role) { $content .= $role->name . "<br/>"; } } } elseif ( $input->get->logout ) { // page was accessed with ?logout=1 GET variable, so log them out $session->logout(); $content = $form; } else { // user arrived at login page for first time $content = $form; } Link to comment Share on other sites More sharing options...
flydev Posted February 4, 2017 Share Posted February 4, 2017 11 hours ago, Lance O. said: [...] if ( $session->login($input->post->user, $input->post->pass) ) { [...] The login function returns an user object on successful login or null on failure. You need to check this returned user object for roles: [...] $loggeduser = $session->login($input->post->user, $input->post->pass); if ( $loggeduser->isLoggedIn() ) { // user was authenticated and logged in // user has "admin" or "superuser" role foreach ($loggeduser->roles as $role) { $content .= $role->name . "<br/>"; } } [...] PS: do not forget to sanitize your input. 5 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now