• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Change Password

Hi i have created a login script using the tutorial follwing:
http://www.phpfreaks.com/tutorials/40/0.php

it send a generated password to the user via email but does not inlude a script to change the password after activation of the email address and username. Take a look at the link so you can see how the code works. I have got some code but get the follwing error:

Parse error: parse error, unexpected T_STRING in /changepw.php on line 10

Code: this code is called from a form on a html page
-----------------------------------------------changepw.php--------------------------------------

<?
function change_pass($old_pass1, $new_pass1, $new_pass2, $user) {
$oldpass = md5($old_pass1);
$queryoldpass = mysql_query("SELECT PASSWORD FROM users WHERE username = ".$user." AND password = ".$oldpass."");
if($new_pass1 == $new_pass2) {
   if($queryoldpass) {
      $newpass = md5($new_pass1);
        $updatepass = mysql_query("UPDATE PASSWORD FROM users WHERE PASSWORD = ".$oldpass." VALUES ('".$newpass."')";
      if(!$updatepass="") {
         echo "Error!";
         exit;
      } else {
         echo "Password Updated!";
      }
   } else {
      echo "Paswords DO NOT Match!";
   }
}
?>
-------------------------------------------------------------------------------------------



Thanks,

Web_Dev
0
web_dev
Asked:
web_dev
  • 8
  • 4
1 Solution
 
snoyes_jwCommented:
exit is a function, and should therefore be written exit().  But that's line 11, so try it and post again if you get the same error.
0
 
web_devAuthor Commented:
yeh that worked i got an error on line 8 saying unexpected ; so i took that out and now i get the following error

Parse error: parse error, unexpected T_IF in /changepw.php on line 9
0
 
snoyes_jwCommented:
There should be a ; at the end of the line; could be a mismatched quotes problem.  PHP will expand variables inside double quotes, so jumping in and out of double quotes is not necessary.  Your update query is not legal syntax, and even if it were, it would change everybody's password that happened to be the same, which would definitely upset some of the other users (just think of how many of your users are going to use "PASSWORD" for their password)

You're also going to have a problem with the logic of testing whether or not the old password matches, because your "if" statement is looking at whether mysql returns a result set at all, even if it's empty.

This might work a little better:

<?
function change_pass($old_pass1, $new_pass1, $new_pass2, $user) {
    if($new_pass1 == $new_pass2) {
        $queryoldpass = mysql_query("SELECT username FROM users WHERE username = '$user' AND password = '$oldpass'") or die(mysql_error());
        if(mysql_num_rows($queryoldpass) > 0) {
            $newpass = md5($new_pass1);
            mysql_query("UPDATE users SET password = '$newpass' WHERE username = '$user'") or die(mysql_error());
            echo "Password Updated!";
        }
        else
            echo "Wrong username/password";
    }
    else
        echo "Paswords DO NOT Match!";
}
?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
web_devAuthor Commented:
ok i implemented your code and all i get is a blank page back....
0
 
snoyes_jwCommented:
Are you calling the function?
0
 
web_devAuthor Commented:
from where?
0
 
web_devAuthor Commented:
and how?
0
 
snoyes_jwCommented:
Someplace you have a form that prompts the user for their user name, old password, and new password, perhaps in a file called getnewpassword.html.  Then you submit that form to some page, perhaps called changepassword.php, which includes the function we're writing in this post.  Then you have to call the function with the variables from the form, something like

extract($HTTP_POST_VARS);
change_pass($old_pass, $new_pass1, $new_pass2, $user);

where old_pass, new_pass1, new_pass2, and user are the names of the form elements from the previous page (e.g., you have <input type="password" name="old_pass">)
0
 
web_devAuthor Commented:
where do i put that in the form or top of the page? .....
0
 
web_devAuthor Commented:
right i call the function at the top of the php page and it just displays wrong user/pass when details are correct i have check everything now cant change anything
0
 
web_devAuthor Commented:
Hi i found out why it keeps displaying wrong username/password. I changed the password in the DB so it is not encrypted and it successfully changed the password. it doesn't match the old password with the encrypted password. how do i do that?
0
 
web_devAuthor Commented:
Dont worry now i worked it out...what i done was to put

$oldpass = md5($old_pass1);

before the

$queryoldpass = mysql_query("SELECT username FROM users WHERE username = '$username' AND PASSWORD = '$oldpass'") or die(mysql_error());

Thanks for your help...

Web_Dev
     
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now