TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 05-07-2008, 08:55 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

After this line:

PHP Code:
$result mysql_query($query); 
you don't need to go through the result (which, btw, should be only one). Instead, you can easily do this:

PHP Code:
if(mysql_num_rows($result) > 0)
{
    echo 
'successfully logged in!';
}
else
{
    echo 
'wrong username and/or password';


And an advice: don't store the password in clear text in your database. Instead, use a hashing algorithm like md5 or sha1.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Jmz (05-07-2008)
Old 05-07-2008, 09:11 PM   #3 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thanks for that
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 05-07-2008, 09:36 PM   #4 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Ok, now I'm stuck with the next part.

Once the username and password are matched with one from the db I want to set the users username as a session variable. So I put:
PHP Code:
session_start();
$r=mysql_fetch_array($result);
$_session['UserName'] = $r['UserName'];
echo 
$_session['UserName'];
echo 
"<br />";
echo 
'<a href="2.php">Next</a>'
Then on the page 2.php I add:
PHP Code:
<?php
session_start
();
echo 
$_session['UserName'];
?>
Should this not display the users username on the page?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 05-07-2008, 10:05 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

$_SESSION is what you want to set, not $_session.
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Jmz (05-08-2008)
Old 05-08-2008, 06:42 AM   #6 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Great, thanks.

Now, does anybody have a link to a tutorial that will show me how to make sure my sessions are secure?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 05-08-2008, 10:47 AM   #7 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

On the left of this page, Securing your PHP applications Part 1 has a piece of code to check the SESSION. Insert that in your script, you might also want to check IP for differences.
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:50 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design