Cant get PHP login page to work

I am trying to create a basic login page using php. I have tried to keep it simple at first. Here is a screenshot of the table in SQL Server:

SQl table
Here is the code:

<?php error_reporting(E_ALL);?>
<!DOCTYPE html>
<html>
<head>
  <title>Portal2 Login</title>
</head>
<body>

<?php
//Connection string
require_once("includes/conn_intranet.php")?>

<?php
// Starting Session
session_start();

// Variable To Store Error Message
$error='';

// Define $txtUsername and $txtPassword
$txtUsername=$_POST['txtUsername'];
$txtPassword=$_POST['txtPassword'];

echo "Username is " . $txtUsername . "</br>";
echo "Password is " . $txtPassword . "</br>";

//Query employee table
$query = "SELECT * FROM employee WHERE ename='$txtUsername'";
echo "Query is " . $query . "</br>";
$results = sqlsrv_query($connIntranet, $query);
$row = sqlsrv_fetch_array($results);

echo "Row ename " . $row["ename"] . "</br>";
echo "Row password " . $row["password"] . "</br>";

if($row["ename"]==$txtUsername && $row["password"]==$txtPassword){
    //$_SESSION['login_user']=$txtUsername;
    //header("location: index.php");
    $error = "Information matched";}
else {
    $error = "Username or Password is invalid";
}
echo $error;

sqlsrv_close( $connIntranet );
?>

<!-- Login Form -->
<div style="text-align: center;">
<form name="frmLogin" action="" method="post">
<h3>Login to access the Portal2</h3>
<p>Username: <input name="txtUsername"></p>
<p>Password: <input name="txtPassword"></p>
<p><input name="btnSubmit_login" type="submit" value="Submit"></p>
</form>
</div>

</body>
</html>

Open in new window


Whenever I try to login using a correct username and password it tells me "username or password is invalid".  You can see where I echod the information from the textboxes, the query, and the row results from the query. See image:
Correct credentials
Whenever I use a blank username and password it tells me "Information Matched".  See image:
Blank credentials
This is not how this is supposed to work. Any ideas?
Robert FrancisDirector of Continuous ImprovementAsked:
Who is Participating?
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.

Dave BaldwinFixer of ProblemsCommented:
If you are going to use Sessions then 'session_start();' must be at the top of the page.  Where it is you should be getting errors already.  It should be like this:
<?php 
error_reporting(E_ALL);
session_start();
?>

Open in new window

1
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Dave - The session start is commented out so I don't think that is the problem
Ray - I am looking into that article now. I would still like to know what is wrong with the code I have now. It just seems so straight forward. It might help me in the future.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Robert FrancisDirector of Continuous ImprovementAuthor Commented:
My bad. The session start was not commented out. I fixed that but still very confused as to why this code is not working.
0
Ray PaseurCommented:
PHP sessions may fail to work if there is any browser output at all (even invisible whitespace) before session_start().
http://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html

The reason for this is that PHP sessions set HTTP cookies, and HTTP cookies are headers.  Headers must come first and be complete before any browser output at all.
0
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Ray. Cool. I have the session stuff fixed based off the article you sent. But I still want to know what is going on with the "Username is Invalid" problem.
0
Ray PaseurCommented:
I can't tell from here, but I can offer a suggestion.  This appears to be a data-dependent problem, so it's important to visualize the data.  Try using var_dump() to print out the data elements that are in play.  The best places to use var_dump() are any place where you cannot see the variables, for example, in $_POST.  Or anywhere that your assumptions are not validated.  Example: If you think $a == $b, but the program logic seems to indicate that they are not equal, var_dump($a, $b) will show you what the PHP script is looking at.
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
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
I found the problem. The datatype I was using was creating whitespace at the end of the usernames and passwords.
0
Ray PaseurCommented:
PHP trim() may be your friend :-)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.