Author Topic: [SOLVED] Hash Password Help!  (Read 482 times)

0 Members and 1 Guest are viewing this topic.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
[SOLVED] Hash Password Help!
« on: June 26, 2009, 04:12:01 PM »
I already scripted a login/registration script, but I didn't know about password hashing. Now I'd like to know what I need to do to make a script that hashes existing and future passwords...  :-[

Offline WolfRage

  • Devotee
  • Gender: Male
    • View Profile
    • Feral Bytes
Re: Hash Password Help!
« Reply #1 on: June 26, 2009, 04:29:12 PM »
http://www.phpfreaks.com/forums/index.php/topic,254277.0.html
Let me know if you need any more infor than this. But if you read it you will be more knowledgable then you had ever wished to be about hashing. Includes plenty of hash examples.
-- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * --
Please be forewarned; rather than giving you exactly what you want I prefer to teach you how to get what you want. Knowledge is power, so take the time to learn PHP and you will be able to wield it's power.
If I just gave you the code to solve your problem then you will be back again tomorrow asking for more of the same code. So please take the time to learn. Thanks.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #2 on: June 26, 2009, 05:14:02 PM »
Amazing. I'll look through it and get back. :)

Offline SetToLoki

  • Enthusiast
    • View Profile
Re: Hash Password Help!
« Reply #3 on: June 26, 2009, 05:29:08 PM »
I already scripted a login/registration script, but I didn't know about password hashing. Now I'd like to know what I need to do to make a script that hashes existing and future passwords...  :-[


I would use the sha1 function http://uk3.php.net/sha1
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
while ($HORSE == $DEAD) beat();
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #4 on: June 30, 2009, 10:15:51 AM »
Code: [Select]
UPDATE users SET password = md5(password);
I was given that code, and told that it would encrypt existing passwords. I was wondering if that is true, it's an SQL statement so it would just update my table, right?


Now how about encrypting passwords in a login script/ registration script? I looked through that link to the tutorial, but there is so much info for me to decipher. Any help?  :)

Offline WolfRage

  • Devotee
  • Gender: Male
    • View Profile
    • Feral Bytes
Re: Hash Password Help!
« Reply #5 on: June 30, 2009, 12:04:16 PM »
I don't know about the SQL statement, but seems to be correct so long as they support that function. You can do the same thing with PHP prior to inserting it into the database. Just remember Hashing is different than encrypting. Hash is irreversible. Encryption can be reversed, so make sure you have the right one for your task.
-- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * --
Please be forewarned; rather than giving you exactly what you want I prefer to teach you how to get what you want. Knowledge is power, so take the time to learn PHP and you will be able to wield it's power.
If I just gave you the code to solve your problem then you will be back again tomorrow asking for more of the same code. So please take the time to learn. Thanks.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #6 on: June 30, 2009, 12:19:45 PM »
Okay :)

Now how would I include that in a registration script so that passwords are automatically hashed or encrypted when they are stored to the database?

I can't add that into config.php (where the script connects to the database), can I? Won't that change all the passwords over?

Offline premiso

  • Life is too short
  • Freak!
  • Gender: Male
  • Liquid luck is Jack Daniel's
    • View Profile
Re: Hash Password Help!
« Reply #7 on: June 30, 2009, 12:23:46 PM »
That is hashing, not encrypting. I would avoid using the MySQL MD5 statement, as you are a bit more limited, with PHP you can salt the MD5 hash a bit better to make it more secure. Either way you go, do the same through out the whole script. If a user logins and you are verifying the login, use the MySQL MD5 or PHP MD5, do not mix and match as it will generate difference results.

SHA1 is a bit stronger, but yea. I would avoid using the MySQL MD5 function, just because I see it as being a bit more limited in how you control it.

Now how about encrypting passwords in a login script/ registration script? I looked through that link to the tutorial, but there is so much info for me to decipher. Any help?  :)


Please be a bit more descriptive of what you want to know. When a user signs up you hash the password and store that in a database, when a user logins you hash the password they gave you for the user and check it against your database to verify they match. Pretty simple process.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #8 on: June 30, 2009, 12:50:10 PM »
Okay, so I should look into using sha1()? I saw it, but wasn't sure...MD5 looked simpler.

This is want I need to know:

a) How to hash existing passwords in my database (is the SQL statement above correct ^^^? )

b) What kind of code I need to insert into my login/registration scripts to hash a password. So, if someone registers, what do I need to do to have the password they enter hashed? Same with login so that they will match.


Thanks :D

Offline corbin

  • Guru
  • Freak!
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #9 on: June 30, 2009, 12:54:09 PM »
a.  Yes, that query will work.  Make sure you only run it once though or you will hash everything twice.

b.  md5()
Why doesn't anyone ever say hi, hey, or whad up world?

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #10 on: June 30, 2009, 01:21:57 PM »
Thanks. ;D

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #11 on: June 30, 2009, 03:39:48 PM »
Okay, I need to know if it's supposed to look something like this...

REGISTRATION SCRIPT
Code: [Select]
<?php 
ini_set 
("display_errors""1");
error_reporting(E_ALL);
include(
"config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

// check if the username is taken
$check "select id from $table where username = '".$_POST['username']."';"
$qry mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 
if (
$num_rows != 0) { 
echo 
"Sorry, there the username {$_POST['username']}  is already taken.<br/>";
echo 
"<a href=user_login.php>Try again</a>";
exit;
}
if(
$_POST['password'] != $_POST['confirmnewpassword']){ 
   echo (
"The two passwords must match. <br/>"); 
echo (
"<a href=user_login.php>Try Again</a>");
}
else {

// insert the data
$insert mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."')")
or die(
"Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"
echo 
"Now you can <a href=user_login.php>log in</a>"
}

?>

 
THE HASH PART

Code: [Select]
<?php 
 
$password 
$_POST['password'];

$hashed md5($password); 

if (
$hashed == $_POST['password'] { 
exit; 
}
?>


I think it's way off, but I've been staring down md5() and hashing manuals for hours and my brain hurts.

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #12 on: June 30, 2009, 05:54:11 PM »
OK, let's break down what you need into actionable steps. As premiso stated I would suggest using a salted hash so if someone was able to get your hashed values they can't use a lookup table to determine the passwords. A salt is just some manner of modifying the value in a consistent manner before hashing. By creating a value that is not 'common' it makes it significantly less likely that someone would have the value in a lookup table. You can salt a value by appending a value (such as the username), reversing the string, or anything that you can consistently replicate.

So, here are the steps I would take:

1) Create a function to generate a salted hashed password.
2) Create a simple PHP page to run that function on all the current passwords (see caution below)
3) Modify the user creation script to hash the password (using the above function) before doing the SQL Insert
4) Modify the login script to hash to provided password (using the above function) before comparing it against the value in the database.

Example hashing function: appends the username to the passoword before hashing to prevent the use of lookup tables if someone got a hold of the value. You could also do something such as reverse the characters of the string or anything that you can consistently reproduce but will result in a value that would not be 'common'.
function hashPW($password$username)
{
    return 
sha1($password.$username);
}


Sample function to update current records (note this may take a while if you have a LOT of records)
//Be sure to include the hash function!
$query "SELECT id, username, password FROM users";
$result mysql_query($query);
$values = array();
while (
$record mysql_fetch_assoc($result))
{
    
$query "UPDATE users
              SET password ='" 
hashPW($record['password'], $record['username']) . "'
              WHERE id = 
{$record['id']}";
    
mysql_query($query);
}
You should test this to ensure it works before running. Plus, you might want to backup your database first. It would be bad news if the script fails and you could not return the values to their original values. You could also insert the hashed value into a temporary column.

Update the login script to hash entered password before comparing against the db value.
--No code provided

Update the Registration script to hash the password
-Change this
// insert the data
$insert mysql_query("insert into $table values ('NULL', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."')")
or die(
"Could not insert data because ".mysql_error());


-To this
$password hashPW($_POST['password'], $_POST['username']);
// insert the data
$insert mysql_query("insert into $table values ('NULL', '{$_POST['username']}', '{$password}', '{$_POST['email']}')")
or die(
"Could not insert data because ".mysql_error());


Lastly, you do not need to worry about protecting the password against sql injection since you are hashing the value - but you definitly want to use mysql_real_escape_string() for the username and email.
« Last Edit: June 30, 2009, 05:56:54 PM by mjdamato »
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #13 on: June 30, 2009, 07:28:15 PM »
You, my friend, are God. :D

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #14 on: June 30, 2009, 08:04:08 PM »
PS Here's the login script:

Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.$_POST['password']."';"

$qry mysql_query($match)
or die (
"Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 

if (
$num_rows <= 0) {
echo 
"Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo 
"<a href=log.php>Try again</a>";
exit; 
}
else {
setcookie("loggedin""TRUE"time()+(3600 24));
setcookie("mysite_username""{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}
?>

I don't have a great enough understanding of PHP to be able to modify this. :D

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #15 on: June 30, 2009, 09:23:29 PM »
Simply change this
$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.$_POST['password']."';";


To this

$match 
"select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";


Again, you REALLY need to be using mysql_real_escape_string() on user submitted values used within a query. I cannot stress this enough. All it would take is one "proplem" input, intentional or accidental, to destroy your database.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #16 on: June 30, 2009, 09:27:14 PM »
Really?  :o
What is this mysql_real_escape_string()?
I'll go research it. :D

Thanks, I'll post the final codes after I'm done.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #17 on: June 30, 2009, 09:46:30 PM »
This is what I've gotten:

The Function
Code: [Select]
<?php
include "config.php"


// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

function 
hashPW($password$username)
{
    return 
sha1($password.$username);
}
//Be sure to include the hash function!
$query "SELECT id, username, password FROM users";
$result mysql_query($query);
$values = array();
while (
$record mysql_fetch_assoc($result))
{
    
$query "UPDATE users
              SET password ='" 
hashPW($record['password'], $record['username']) . "'
              WHERE id = 
{$record['id']}";
    
mysql_query($query);
}
?>

Updated Login Script

Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());


$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";

$qry mysql_query($match)
or die (
"Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 

if (
$num_rows <= 0) {
echo 
"Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo 
"<a href=log.php>Try again</a>";
exit; 
}
else {
setcookie("loggedin""TRUE"time()+(3600 24));
setcookie("mysite_username""{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}
?>

The Updated Registration Script

Code: [Select]
<?php 
ini_set 
("display_errors""1");
error_reporting(E_ALL);
include(
"config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

// check if the username is taken
$check "select id from $table where username = '".$_POST['username']."';"
$qry mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 
if (
$num_rows != 0) { 
echo 
"Sorry, there the username {$_POST['username']}  is already taken.<br/>";
echo 
"<a href=user_login.php>Try again</a>";
exit;
}
if(
$_POST['password'] != $_POST['confirmnewpassword']){ 
   echo (
"The two passwords must match. <br/>"); 
echo (
"<a href=user_login.php>Try Again</a>");
}
else {

$password hashPW($_POST['password'], $_POST['username']);
// insert the data
$insert mysql_query("insert into $table values ('NULL', '{$_POST['username']}', '{$password}', '{$_POST['email']}')")
or die(
"Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"
echo 
"Now you can <a href=user_login.php>log in</a>"
}

?>

I just want to check and make sure it's all good. Thanks in advance to anyone that helps :)

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #18 on: June 30, 2009, 09:51:12 PM »
I don't see where you have included the hashing function on the login or registration scripts. That is why I created a function - it will ensure you are slating/hashing the values exactly the same every time. You should put it in an external file and include() it on those pages - never copy and paste a function into multiple pages. You will eventually update one and not the other some day.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #19 on: June 30, 2009, 10:33:28 PM »
Oh, I didn't know it needed to go into every page, I'll do that now.

Other than that it looks all good?

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #20 on: July 01, 2009, 01:15:34 PM »
These are my final codes, well, the ones I came up with that are probably wrong. :P

function.php

Code: [Select]
function hashPW($password, $username)
{
    return sha1($password.$username);
}

hash.php [for hashing passwords]
Code: [Select]
<?php
include "config.php"


// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

include 
"function.php"

//Be sure to include the hash function!
$query "SELECT id, username, password FROM users";
$result mysql_query($query);
$values = array();
while (
$record mysql_fetch_assoc($result))
{
    
$query "UPDATE users
              SET password ='" 
hashPW($record['password'], $record['username']) . "'
              WHERE id = 
{$record['id']}";
    
mysql_query($query);
}
?>

register.php
Code: [Select]
<?php 
ini_set 
("display_errors""1");
error_reporting(E_ALL);
include(
"config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

// check if the username is taken
$check "select id from $table where username = '".$_POST['username']."';"
$qry mysql_query($check) or die ("Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 
if (
$num_rows != 0) { 
echo 
"Sorry, there the username {$_POST['username']}  is already taken.<br/>";
echo 
"<a href=user_login.php>Try again</a>";
exit;
}
if(
$_POST['password'] != $_POST['confirmnewpassword']){ 
   echo (
"The two passwords must match. <br/>"); 
echo (
"<a href=user_login.php>Try Again</a>");
}
else {
include 
"function.php";
$password hashPW($_POST['password'], $_POST['username']);
// insert the data
$insert mysql_query("insert into $table values ('NULL', '{$_POST['username']}', '{$password}', '{$_POST['email']}')")
or die(
"Could not insert data because ".mysql_error());

// print a success message
echo "Your user account has been created!<br>"
echo 
"Now you can <a href=user_login.php>log in</a>"
}

?>

login.php
Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());


$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";
include 
"function.php"

$qry mysql_query($match)
or die (
"Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 

if (
$num_rows <= 0) {
echo 
"Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo 
"<a href=log.php>Try again</a>";
exit; 
}
else {
setcookie("loggedin""TRUE"time()+(3600 24));
setcookie("mysite_username""{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}
?>


I really need to know if these are correctly formatted and stuff so I can re-open my site. Thanks :D

Offline corbin

  • Guru
  • Freak!
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #21 on: July 01, 2009, 01:21:41 PM »
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "{$_POST['username']}");



Oh... Errr....


Never trust cookies.  Having a "loggedin" cookie set to true is a horrible thing security wise.  Cookies can easily be manipulated.  Instead, set a session value or something.


Also, if you ever want to have a "Remember Me" feature, put the username and some token that a user could not know without actually having done it legitly.
Why doesn't anyone ever say hi, hey, or whad up world?

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #22 on: July 01, 2009, 02:25:35 PM »
There's more that is faulty?! :o :D
How would I do this?

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #23 on: July 01, 2009, 02:33:23 PM »
I really need to know if these are correctly formatted and stuff so I can re-open my site. Thanks :D


Cetanu,

I am more than happy to help provide guidance. But, I am not going to test your code for you. YOU need to test the code against your database to ensure it is working correctly. And, noone can categorically state that your code will work with any certainty. I already stated that I didn't see a problem with what you had. Just follow the steps I posted above. But, as I stated you will want to back up your database first before running the process to update current passwords. Then I would test logging in using an existing account to ensure that update process worked. Then lastly test the process of creating a new account and logging in.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #24 on: July 01, 2009, 02:49:52 PM »
Okay, just...I was afraid for me DB. :-/ Didn't think I did it right is all.

But thanks, I'll back it up and test it. :D

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #25 on: July 01, 2009, 03:10:24 PM »
So I've tested the code, and it will not work. The problem lies in here:

Code: [Select]
<?php 
$password 
$_POST['password'];
$username $_POST['username'];

function 
hashPW($password$username)
{
    return 
sha1($password.$username);
}
?>

This is the login script, which I'm trying to fix. :D
Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

include 
"function.php";
$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";
 

$qry mysql_query($match)
or die (
"Could not match data because ".mysql_error());
$num_rows mysql_num_rows($qry); 

if (
$num_rows <= 0) {
echo 
"Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo 
"<a href=log.php>Try again</a>";
exit; 
}
else {
setcookie("loggedin""TRUE"time()+(3600 24));
setcookie("mysite_username""{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}
?>

It gives me my error message about not having username and password specified exist....

I tried it with  {} and without them in the function script.

GOOD NEWS: The existing passwords were hashed and the registration script works. Just the login script is broken.
« Last Edit: July 01, 2009, 03:21:10 PM by Cetanu »

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #26 on: July 01, 2009, 03:28:24 PM »
You need to be more specific. What is the EXACT error message and what line is it giving the error on?

In the first block of code above, why are you defining $password & $username?

I did a test and it outputs exactly what I would expect
$_POST['username'] = 'mjdamato';
$_POST['password'] = 'notmyrealpassword';
$table 'tableName';

function 
hashPW($password$username)
{
    return 
sha1($password.$username);
}

$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";

echo 
$match;

// Output:
// select id
// from tableName
// where username = 'mjdamato'
//   and password = '032aefab39a2f2ee2b90891d62fd19edcd220802';
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #27 on: July 01, 2009, 03:42:31 PM »
There is no error, the page just goes to login.php and is completely blank. The error I was talking about was from the login.php code:
Code: [Select]
echo "Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo "<a href=log.php>Try again</a>";

I cannot define $_POST['username'] or password as "mjdamato" or "notmyrealpassword" because I want that to be gotten from my MySQL server. I defined $username and $password because they weren't defined in the script...and they're supposed to be, yes? I tried a lot just to get this to grab the hashed password from the server and compare it with the typed password, but it was to no avail.
« Last Edit: July 01, 2009, 03:47:23 PM by Cetanu »

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #28 on: July 01, 2009, 03:56:32 PM »
I cannot define $_POST['username'] or password as "mjdamato" or "notmyrealpassword" because I want that to be gotten from my MySQL server. I defined $username and $password because they weren't defined in the script...and they're supposed to be, yes? I tried a lot just to get this to grab the hashed password from the server and compare it with the typed password, but it was to no avail.

I'm not suggesting you define the POST values, that was only a test. And, post values do not come from the database, they come from a form post. are you even sure of what your code does?

I defined $username and $password because they weren't defined in the script...and they're supposed to be, yes?

No. you pass the values to the function and they are defined within the function.

Echo the value of $match to the page to see if the query is being generated as you expect.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #29 on: July 01, 2009, 04:02:54 PM »
Well I sort of have an idea what the code's doing, but this is really a learn-as-you-go experience for me. That's what PHP has been so far, and I think I've learned a fair bit.  ;D

I'll see what happens when I echo $match.
Thanks.

EDIT: It does echo what I want it to!
With this:
Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

include (
"function.php");
$match "select id from $table where username = '".$_POST['username']."'
and password = '"
.hashPW($_POST['password'],$_POST['username'])."';";

echo 
$match;
?>

So it would have to be something in here:
Code: [Select]
...
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);

if ($num_rows <= 0) {
echo "Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo "<a href=user_login.php>Try again</a>";
exit;
}
else {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}
?>

Right?
« Last Edit: July 01, 2009, 04:10:45 PM by Cetanu »

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #30 on: July 01, 2009, 05:49:59 PM »
Use this code and run a test. Then post the text displayed
Code: [Select]
<?php

include("config.php"); 

// connect to the mysql server
$link mysql_connect($server$db_user$db_pass)
or die (
"Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die (
"Could not select database because ".mysql_error());

include (
"function.php");
//$match = "select id from $table where username = '".$_POST['username']."'
//and password = '".hashPW($_POST['password'],$_POST['username'])."';";

//Test Query
$match "select password from $table where username = '".$_POST['username']."'";

$qry mysql_query($match)
or die (
"Could not match data because ".mysql_error());

//----BEGIN TEST CODE
$result mysql_fetch_assoc($qry);
echo 
"QUERY: {$match}<br />
POSTED VALUES:<br />
 - Username: 
{$_POST['username']}<br />
 - Password: 
{$_POST['password']}<br />
 - Hashed Password: " 
hashPW($_POST['password'], $_POST['username']) . "<br />";
echo 
"Database Password: {$_result['password']}";
exit();
//-----END TEST CODE

$num_rows mysql_num_rows($qry); 

if (
$num_rows <= 0) {
echo 
"Sorry, there is no username, {$_POST['username']}, with the specified password.<br/>";
echo 
"<a href=user_login.php>Try again</a>";
exit; 
}
else {
setcookie("loggedin""TRUE"time()+(3600 24));
setcookie("mysite_username""{$_POST['username']}");
header('Location:http://mythscape.freezoka.com/');
die();
}

?>
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #31 on: July 01, 2009, 08:38:26 PM »
Okay:

QUERY: select password from users where username = 'Admin'
POSTED VALUES:
- Username: Admin
- Password: ------
- Hashed Password: 89cda54482caa109b5544b204b0ad06a7d57df4e
Database Password:

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #32 on: July 01, 2009, 08:57:22 PM »
Okay, I found that a variable was improperly defined and then retried it so this is the final after my change:

QUERY: select password from users where username = 'Admin'
POSTED VALUES:
- Username: Admin
- Password: ------ [<< I changed it to that, it showed my password]
- Hashed Password: 89cda54482caa1e9b5544b204b0ad06a7d57df4e
Database Password: 89cda54482caa1e9b5544b204b0ad06a

Offline mjdamato

  • Guru
  • Fanatic
  • *
  • Gender: Male
    • View Profile
Re: Hash Password Help!
« Reply #33 on: July 01, 2009, 09:14:14 PM »
Check the definition for the password field in the database. I'm guessing you set the length of that field to 32 characters - so the value is getting truncated (i.e. the last 8 characters are getting cut off)!

You will need to:

1. Restore your backed up database
2. Increase the length of the field in the database to at least 40 characters
3. Rerun the script to hash the current passwords

Is "should" all work then. Now aren't you glad you made a backup of the database?!
 
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #34 on: July 01, 2009, 11:10:19 PM »
Wait, the database field is set to be too short to be hashed? Okay, I can change it. YES I am happy that I backed it up. I'll go do that, thanks :D

I'll pro'ly have one or two more questions.

Offline CetanuTopic starter

  • Enthusiast
  • Gender: Male
  • He who can't be killed isn't necessarily immortal.
    • View Profile
    • MythScape
Re: Hash Password Help!
« Reply #35 on: July 02, 2009, 09:53:59 AM »
Okay. Thanks a lot to everyone who helped me accomplish this. It's fixed.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.