What I am trying to do is,
Open the word file, read a line, convert it to md5, write to db if its not already there(both word and encrypted string)
For some reason, it doesnt work.
$file = fopen("list.txt", "r");
while (!feof($file))
{
$word = fgets($file);
$word = mysql_real_escape_string($word); // to prevent injections
$md5string = md5($word);
//check if in DB
$check = mysql_query("SELECT id FROM table WHERE word='$word'") or die(mysql_error
());
if (mysql_num_rows($check) > 0)
{
} //Nothing to do.
else
{
$write = mysql_query("INSERT INTO table (word, md5string) VALUES ('$word','$md5string')") or
die(mysql_error());
}
}
It is making the hashes and storing everything write, but the hashes thats made arent right.
Any idea whats wrong in my code?
Fix : I mysql_real_escaped the string before hashing and that was where I was wrong.
$word = trim(fgets($file));
$md5string = md5($word);
$word = mysql_real_escape_string($word); // to prevent injections`
ADD : I just corrected my script using the suggestions I got from here and found out that
Md5ecnrypter.com is doing it wrong.
They are escaping the string before hashing. How lame. Just checked other sites to see SO suggestions were right.
Try change the order of lines
$word = trim(fgets($file));
$md5string = md5($word);
$word = mysql_real_escape_string($word); // to prevent injections
Thanks mate - Kishor 2012-04-04 17:47
Just taking a stab at this, but the MD5 you are taking is the MD5 of the file after it has been potentially modified by the mysql_real_escape_string() call.
$word = $_POST['word'];
$word = mysql_real_escape_string($word);
$output = md5($word);
Kishor 2012-04-04 17:45
Maybe use md5()
before mysql_real_escape_string()
Um,
Personally I would look for the hash instead, so:
SELECT * FROM table WHERE md5word=$hashed_word
I'd also use trim before md5-ing the word, you don't really need to mysql_real_escape_string() after md5, as md5 turns the data into a hexadecimal number, so, it would be better to do it before, if at all.
Kindly dont misguide - Kishor 2012-04-04 18:14
mysql_real_escape_string
before hashing the word. Also, it could be that the new line character is included when reading from the file - knittl 2012-04-04 17:44