Jump to content

php password change


phprick

Recommended Posts

i made a script to change the password

if i file in the form i get a message that my password is changed

but it doesnt  :'(

please help me

thanks in advanced

if($_POST['submit']=='Set')
{
// If the change form has been submitted
       
       	$err = array();
$usr = $_SESSION['usr'];
        $newpass = $_POST['newpass'];
        $passorig = mysql_query("SELECT pass FROM tz_mebers WHERE usr=$usr");
        $email = mysql_query("SELECT email FROM tz_mebers WHERE usr=$usr");	
if(!$_POST['oldpassword'] == $passorig)
{
	$err[]='Your password is incorrect!';
}

if(!$_POST['newpass'] == $_POST['vnewpass'])
{
	$err[]='Your passwords do not match!';
}

if(!count($err))
{
	// If there are no errors

	$pass = md5($_POST['newpassword']);
	// Generate a random password

	$_POST['newpassword'] = mysql_real_escape_string($_POST['newpassword']);
	// Escape the input data


	mysql_query("UPDATE tz_members SET pass=$newpass WHERE usr=$usr");


                			send_mail(	'member@clephaswebdesign.x10.mx',
					$email,
					'ClephasWebdesign member registration - Your New Password',
					'Your password is: '.$pass);

		$_SESSION['msg']['cha-success']='Your password is set succesfull!';



}

if(count($err))
{
	$_SESSION['msg']['cha-err'] = implode('<br />',$err);
}	

header("Location: home.php");
exit;
}
<form action="" method="post">
				<h1>Change Your Password</h1>		
                                        <?php

					if($_SESSION['msg']['cha-err'])
					{
						echo '<div class="err">'.$_SESSION['msg']['cha-err'].'</div>';
						unset($_SESSION['msg']['cha-err']);
					}

					if($_SESSION['msg']['cha-success'])
					{
						echo '<div class="success">'.$_SESSION['msg']['cha-success'].'</div>';
						unset($_SESSION['msg']['cha-success']);
					}
				?>
                    		
				<label class="grey" >Old Password:</label>
				<input class="field" type="password" name="oldpass" id="oldpass" value="" size="23" />
				<label class="grey" >New Password:</label>
				<input class="field" type="password" name="newpass" id="newpass" value="" size="23" />
				<label class="grey" >Verify New Password:</label>
				<input class="field" type="password" name="vnewpass" id="vnewpass" value="" size="23" />					
		         	<input type="submit" name="submit" value="Set"  class="bt_register"/>
			</form>

Link to comment
Share on other sites

You're comparing the user inputted field with a query, you know. You need to fetch the row before you can compare its fields.

 

$query = mysql_query("SELECT pass, email FROM tz_mebers WHERE usr=$usr");
$user = mysql_fetch_array($sql);

echo 'Email: '.$user['email'].' Password: '.$user['pass'];

Link to comment
Share on other sites

First and foremost I'd just like to point out that MD5 was found broken in 2006, meaning it would take only one minute to find a collision with a laptop computer back then. So, don't use MD5 for passwords.

Secondly, you'll always want to use a individual salt per user when hashing the password, and change this salt every time the password is changed.

 

Read the thread "What's the point of MD5", and you will be a bit more enlightened. Just remember to read the entire thread, as there are quite a lot of misconceptions posted inside of it. Best advice comes in the final post.

 

Once you've done that, update your code and post the updated version here, if you still have problems with it.

Link to comment
Share on other sites

if(!md5($_POST['oldpassword']) == $passorig)
{
$err[]='Your password is incorrect!';
}

 

The original password should be put through the md5 function to make the comparison.

 

I think this logic is wrong.

Simple example:

 


// incorrect 
$pass = "password";
$passMd5 = md5('password'); 
if(!$passMd5 == md5($pass)){
    var_dump($passMd5);   
}

// correct 
$pass = "password";
$passMd5 = md5('password'); 
if($passMd5 == md5($pass)){
    var_dump($passMd5);   
}

//correct 

$pass = "password";
$passMd5 = md5('password'); 
if(!$passMd5 != md5($pass)){
    var_dump($passMd5);   
}

Link to comment
Share on other sites

Actually, that logic is correct. Provided that he'd tested the correct values, that is. Not that I disagree with you that he should have written it in a more simple manner, to make it easier to read and maintain.

 

The test parses as following:

 // Starting point.
if (!$Hash == md5 ($Pass) {

// Since $Hash and md5 ($Pass) equals, the comparison returns true.
if (!true) {

// Not true == false, thus the IF-block is skipped and we know the password is correct.
if (false) { $err[] = '' }

Link to comment
Share on other sites

Seems I did indeed forget about the operator precedence. Sorry about that.

 

The correct interpretation of how PHP parses it:

 // Starting point.
if (!$Hash == md5 ($Pass) {

// Since ! has precedence, it changes a string to BOOL FALSE.
if (false == md5 (pass)) {

// A string with content is always != false, thus the test will always fail.
if (false) { $err[] = '' }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.