script logging me in even though database user activation value isn't equal to "yes"

This code should only let a user log in if they have activated their account. So, when someone registers the default for the database column is "no". Once they activate it will update to "yes".

$stmt = $link->prepare("SELECT `user_name`, `user_email`, `safe_id`, `access_level`, `user_pass`, `active` FROM `users` WHERE `user_email` = ?");
		$stmt->bind_param("s", $_POST['email']);
		$stmt->execute();
		$result = $stmt->get_result();
		$numRows = $result->num_rows;
		if($numRows > 0) {
			$_SESSION['user_details'] = $result->fetch_object();
			$db_pass = $_SESSION['user_details']->user_pass;
			if(password_verify(trim($_POST['password']), $db_pass) && ($_SESSION['user_details']->active == "yes")) {
// do some other stuff here
} else {
			echo error_message("Your account has not yet been activated");
		}

Open in new window


With this code, when they user tries to login, it shows the error message. If I try again it takes me to the dashboard or if I just press enter in the address bar again it takes me to the dashboard which I can't seem to figure out why.
LVL 1
Black SulfurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Any chance you have a cookie in play?  The PHP session uses a cookie.  Maybe you have multiple windows or multiple tabs open at the same time?  You might want to add a page footer containing var_dump($_SESSION, $_COOKIE);
0
Peos JohnPHPCommented:
Can you print and check session and post values?
print_r($_SESSION);

Open in new window

and
print_r($_POST); 

Open in new window

0
Ray PaseurCommented:
See also:

https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html

https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html  * The Fine Print

If you read those articles and understand them, and that's still not an adequate explanation, please post back and show us the var_dump() data requested above.
0
Black SulfurAuthor Commented:
I solved the mystery. I had some code that I put on the login page which was meant to check if the user was already logged in. If they were logged in then instead of seeing the login page it should have taken them to their dashboard. I removed that code and now it works as expected. I will just have to relook at this code and figure out how to fix it.

function already_loggedin() {
	
	if(isset($_SESSION['user_details']->safe_id) || isset($_COOKIE['r_token'])) {
		
		header("location: dashboard.php");
		exit();
	}
} // end check if user already logged in.

Open in new window


I don't really get this though because even with an incorrect password it was taking me to the dashboard page. This session would only be set if the password was correct

if(isset($_SESSION['user_details']->safe_id) 

Open in new window

0
Ray PaseurCommented:
even with an incorrect password it was taking me to the dashboard
Right, that's why I asked about cookies!

This may happen because of one of two factors, and either one will cause it.

Factor one:
isset($_SESSION['user_details']->safe_id)

Factor two:
isset($_COOKIE['r_token'])

Since the test is with || you're looking for either of these factors, not both.  It's usually a good design to have only one place to look for a fact like login status.

There's a solution set that works correctly in this article.  It might be a good idea to lay a foundation, then build up your application on top.
https://www.experts-exchange.com/articles/2391/PHP-Client-Registration-Login-Logout-and-Easy-Access-Control.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.