Page 1 of 1

help with Md5 password code

Posted: Wed Oct 08, 2008 5:13 pm
by me666
i have a table with info into about users for my site, people can join fine with md5 as i used an example to do that but i have added a few bits to it. I am trying to make it so you can update certain parts, i can allow people to update anything in th table now except their password.... i can have them update it but not with md5 meaning the new password wont work.... here is how i update without using md5...

Code: Select all

<?php
$name = $_SESSION[username];
?>
<form>New Password: <input type="text" name="pass" maxlength="50"><input type="submit" name="edit" value="OK"></form>
<?php mysql_query("update users set password='$pass' WHERE username = '$name'");
?>
any ideas on how to change this to encrypt the new password to md5?

you can get me on leo@leos-webby.xf-s.com

Re: help with Md5 password code

Posted: Wed Oct 08, 2008 5:43 pm
by VladSun
Use the native MySQL function MD5()
:)

Re: help with Md5 password code

Posted: Wed Oct 08, 2008 5:56 pm
by pickle
Strictly speaking, md5 is hashing (one way), not encryption (two way).

Re: help with Md5 password code

Posted: Wed Oct 08, 2008 6:01 pm
by me666
so ur saying to change it to...

Code: Select all

<?php
$name = $_SESSION[username];
?>
<form>New Password: <input type="text" name="pass" maxlength="50"><input type="submit" name="edit" value="OK"></form>
<?php mysql_query("update users set password='MD5($pass)' WHERE username = '$name'");
?>
???
this then changes the pass word but when i try tologin it says my password is incorrect :S i think i did try that.... im new to this lol but cnt figure it out

Re: help with Md5 password code

Posted: Thu Oct 09, 2008 4:22 am
by VladSun
Look at the (single!) example found in the manual I've point it to you.

Re: help with Md5 password code

Posted: Sat Oct 11, 2008 4:25 am
by me666
i wernt sure where you was on about with the example but i have got it now thanks, i looked into it with the md5() function you mentioned, i figured i must use that so tried a few different ways i thought it would work and found a way it would... my code now looks a little like this.....

Code: Select all

<form method="post"><label>New Password: <input type="password" name="new-pass" maxlength="32"></label><br>Are you sure you have entered the correct new password? <input type="checkbox" name="Sure" onclick = "document.getElementById('yes').disabled=false;"><br><input type="submit" id="yes" name="updatepass" value="Change Pass" disabled="true"></form>
       <?php 
       if(isset($_POST['updatepass'])){
       $md5_password = md5($_POST['new-pass']);
       mysql_query("update users set password='$md5_password' WHERE username = '$name'");}
 ?>   
Not sure if this is how you was trying to explain it, but it works ok and does the job.... i have other codes in there so that if ur not logged in you cant even access this page, and ive added a checkbox you need to tick before you can submit it.... thanks for your help :)

Re: help with Md5 password code

Posted: Sat Oct 11, 2008 7:35 am
by VladSun
[quote="me666"]

Code: Select all

if(isset($_POST['updatepass']))
{
       mysql_query("update users set password=MD5('".mysql_real_escape_string($_POST['new-pass'])."') WHERE username = '$name'");
}

Re: help with Md5 password code

Posted: Sat Oct 11, 2008 11:04 am
by me666
sorry i bet u can tell im new to php and mysql, first project this lol....
so if i just change mine to what you have put there, thats how it is supposed to be right?

thanks agen for all ur help :)