login.pp - Can't log in wrong username/password

+1 gone bye · November 13, 2014
So i'v been following along on the PHP tuts for register & login im a quarter the way through but i cant stand to go further without fixing this error i keep getting...
first off the login has been made, the user is created and there are no DB problems, but when i log in with the correct username and pasword i get an error. please help especially if you have completed these tuts....
include 'core/init.php';

if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password.';
} else if (user_exists($username) === false) {
$errors[] = 'Username not found.';
} else if (user_active($username) === false) {
$errors[] = 'You haven\'t activated your accound.';
} else {

if (strlen($password) > 32) {
$errors[] = 'Password too long.';

$login = login($username, $password);
if ($login === true) {
$errors[] = 'Username or password is incorrect';
} else {
$_SESSION ['user_id'] = $login;
header('Location: index.php');


} else {
$errors[] = 'No data received';

if (empty($errors) === false) {
<h2> We tried to log you in but...</h2>
echo output_errors($errors);



require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

if (logged_in() === true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email');
if (user_active($user_data['username']) === false) {
header('Location: index.php');


$errors = array();


Post a Reply


Oldest  Newest  Rating
0 Ron Butcher · November 14, 2014
Can you post your "login" function?  It looks like you have the if statement reversed after running that function.  If it returns true (assuming true means it found a match) you give a username and password error, if it returns false (assuming it did not find a match), you log the user in.

I would like to see the function though, I'm assuming you have it backwards based on how I would do it.
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.