50
729
Implementing User Functions
When a user clicks on the Change Password button at the bottom of this form, the
store-change-password
action is activated.The code for this action is as follows:
case ‘store-change-password’ :
{
if(change_password(get_email(), $_POST[‘old_passwd’],
$_POST[‘new_passwd’], $_POST[‘new_passwd2’]))
{
echo ‘<p>OK: Password changed.</p>
<br /><br /><br /><br /><br /><br />’;
}
else
{
echo ‘<p>Sorry, your password could not be changed.</p>’;
display_password_form();
}
break;
}
As you can see, this code tries to change the password using the
change_password()
function and reports success or failure to the user.The
change_password()
function,
shown in Listing 30.13,can be found in the
user_auth_fns.php
function library.
Listing 30.13 change_password()Function from user_auth_fns.php—This
Function Validates and Updates a User’s Password
function change_password($email, $old_password, $new_password,
$new_password_conf)
// change password for email/old_password to new_password
// return true or false
{
// if the old password is right
// change their password to new_password and return true
// else return false
if (login($email, $old_password))
{
if($new_password==$new_password_conf)
{
if (!($conn = db_connect()))
return false;
$query = “update subscribers
set password = sha1(‘$new_password’)
where email = ‘$email’”;
$result = $conn->query($query);
return $result;
}