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 12-11-2008, 12:02 PM   #1 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default Password Hashing

Hey I'm having some problems with hashing passwords into a database and then comparing them to a user input.

Here's my code:
PHP Code:
if(isset($_REQUEST['username'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());


$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = md5($_POST['password']);
$result mysql_query("SELECT Password FROM Users WHERE Username = '$username'");
$row mysql_fetch_array($result);
$num mysql_num_rows($result);

if(
$num != "0" && $password == $row['Password']) {
header('location:new.php');
}
else {
session_unset();
session_destroy();
}

mysql_close($con);
}
if(isset(
$_REQUEST['userReg'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());

$_SESSION['userReg'] = $_POST['userReg'];
$_SESSION['passReg'] = md5($_POST['passReg']);
$sql mysql_query("INSERT INTO Users
(Username, Password) VALUES('
$userReg','$passReg')") or die(mysql_error());
echo 
"You are now registered!";

mysql_close($con);

I have the script running fine without hashing the passwords, just when I add the MD5 function it goes wrong.

I do manage to hash the password into the database but it doesn't work when the user logs in.

Also it doesn't hash the password every time. Sometimes it just enters the password as plain text into the database.

Any help would be appreciated. :)
Normo is offline  
Reply With Quote
Old 12-11-2008, 01:01 PM   #2 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

It seems to me that you are setting the password with encryption into the session here

$_SESSION['password'] = md5($_POST['password']);

and then trying to check a variable called password here.

if($num != "0" && $password == $row['Password']) {

You do this in the register as well. This is the problem. There is no need to store that stuff in the session for now just setting it to the variable would work. Otherwise your logic seems good.
CoryMathews is offline  
Reply With Quote
Old 12-11-2008, 05:43 PM   #3 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default

Thanks that does make sense, however even when I take out the sessions it still doesn't work! It just says says "Username or Password was wrong" although I can't figure out why?! :S

PHP Code:
if(isset($_REQUEST['username'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());

$_SESSION['username'] = $_POST['username'];
$username $_POST['username'];
$password md5($_POST['password']);
$result mysql_query("SELECT Password FROM Users WHERE Username = '$username'");
$row mysql_fetch_array($result);
$num mysql_num_rows($result);

if(
$num != "0" && $password == $row['Password']) {
header('location:new.php');
}
else {
session_unset();
session_destroy();
}

mysql_close($con);
}
if(isset(
$_REQUEST['userReg'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());

$userReg $_POST['userReg'];
$passReg md5($_POST['passReg']);
$sql mysql_query("INSERT INTO Users
(Username, Password) VALUES('
$userReg','$passReg')") or die(mysql_error());
echo 
"You are now registered!";

mysql_close($con);

Normo is offline  
Reply With Quote
Old 12-14-2008, 11:14 AM   #4 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default

Can anyone help with my problem?
Normo is offline  
Reply With Quote
Old 12-14-2008, 01:01 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The code you posted above does not contain the string "Username or Password was wrong" so we can't see the process leading to that message being displayed.
Salathe is offline  
Reply With Quote
Old 12-17-2008, 11:00 AM   #6 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default

Ah sorry here is my entire code:
PHP Code:
<?PHP
if(isset($_REQUEST['username'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());

$_SESSION['username'] = $_POST['username'];
$username $_POST['username'];
$password md5($_POST['password']);
$result mysql_query("SELECT Password FROM Users WHERE Username = '$username'");
$row mysql_fetch_array($result);
$num mysql_num_rows($result);

if(
$num != "0" && $password === $row['Password']) {
header('location:new.php');
}
else {
session_unset();
session_destroy();
}

mysql_close($con);
}
if(isset(
$_REQUEST['userReg'])){
session_start();
$con mysql_connect("localhost","","") or die('Could not connect: ' mysql_error());

mysql_select_db("normo_mydb") or die(mysql_error());

$userReg $_POST['userReg'];
$passReg md5($_POST['passReg']);
$sql mysql_query("INSERT INTO Users
(Username, Password) VALUES('
$userReg','$passReg')") or die(mysql_error());
echo 
"You are now registered!";

mysql_close($con);
}
?>

<html>
<body>

<?PHP if(isset($_REQUEST['username'])){ echo "<b>Username or password was wrong!</b>"; } ?>
<form method="post" action="login.php">
<input type="text" name="username" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
<br />
<br />
<fieldset>
<legend>Register an Account:</legend>
<form method="post" action="login.php">
<input type="text" name="userReg" /><br />
<input type="password" name="passReg" /><br />
<input type="submit" value="Register" />
</form>
</legend>
</fieldset>

</body>
</html>
Normo 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loggining ( password and encryption ) Orc General 9 02-09-2008 08:24 AM
Password generator Rizza Script Giveaway 6 12-11-2007 05:04 AM
Single way password storage DragonBe General 3 11-21-2007 01:42 PM
Forgot Password Gurnk MySQL & Databases 8 11-20-2007 04:06 PM


All times are GMT. The time now is 10:54 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