View Single Post
Old 05-07-2008, 07:56 PM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default Checking a users login details against a database

I'm making a very simple login script, here's what I have so far:

Code:
<?
//Connect to the database
include("../config/connect.php");

//Get the data from the form
$UserName = $_POST['UserName'];
$Password = $_POST['Password'];

//Check the fields arent empty
if(empty($UserName)) {
	echo "Please Enter A Username";
}elseif(empty($Password)){
	echo "Please Enter A Password";
//Theyre all fine, lets continue
}else {	

//Get rid of any nasty inputs
$UserName = mysql_real_escape_string($UserName);
$Password = mysql_real_escape_string($Password);

$query = "SELECT * from tbl_users WHERE UserName = '$UserName' AND Password = '$Password'";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
	echo $row['UserName'];
}
}
?>
It all works fine but I was wondering. What is the best way to see if the users details matched what was in the database or not?

I've tried putting the following in the while loop:
Code:
if(empty($row['UserName'])) {
echo "Not Logged In";
}else{
echo "Logged In";
}
But it doesn't seem to work
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote