douyao1856 2012-04-04 17:41
浏览 12
已采纳

MD5来自文件的单词

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.

  • 写回答

4条回答 默认 最新

  • doq70020 2012-04-04 17:45
    关注

    Try change the order of lines

    $word = trim(fgets($file));
    $md5string = md5($word);
    $word = mysql_real_escape_string($word); // to prevent injections
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化