Right... i've been over and over this so many times and I did spot that I was trying to log in with 'password' and not 'old_password' which ive changed, but still no luck.
ive cleaned the code up a bit so it now looks like this: (a session has been started)
// create short variable names
$old_password = $_POST['old_password'];
$new_password = $_POST['new_password'];
$new_password2 = $_POST['new_password2'];
// check valid user
if (isset($_SESSION['valid_user'])) {
echo 'Logged in as '.$_SESSION['valid_user'].'.<br />';
}
else {
echo 'You are not logged in.<br />[<a href=login.php>login</a>]';
exit;
}
// validate form
if (!filled_out($_POST)) {
echo 'You have not filled the form out completly - please go back'
.' and try again.<br />';
exit;
}
if ($new_password!=$new_password2) {
echo 'The new passwords you entered do not match - please go back'
.' and try again.<br />Your password has not been changed.<br />';
exit;
}
if (strlen($new_password)>16 || strlen($new_password)<6) {
echo 'Your password must be between 6 and 16 characters - '
.'please go back and try again.<br />';
exit;
}
// login with OLD password
// connect to db
$mysql_database="database";
$mysql_username="username";
$mysql_password="pass";
$dbconnect = mysql_connect("localhost",$mysql_username,$mysql_password) or die ("Unable to connect to SQL server");
mysql_select_db($mysql_database,$dbconnect) or die ("Unable to select database");
// check if username is unique
$result = mysql_query("select * from BWmembers where username='$username' and password = sha1('$old_password')")
or die(mysql_errno($result) . ": " . mysql_error($result));
if (mysql_num_rows($result)) {
echo 'Successful Login<br />';
$_SESSION['valid_user'] = $username;
}
else {
echo 'Failed Login<br />[<a href=login.php>login</a>]';
exit;
}
//update password
$result = mysql_query("update BWmembers set password = sha1('$new_password') where username = '$username'")
or die(mysql_errno($result) . ": " . mysql_error($result));
if (!$result) {
echo 'Password could not be changed.';
exit;
}
else {
echo 'Password changed.';
}
the output im getting is:
Logged in as TestUser.
Failed Login
[login]
so it checks that a valid_user is in session, but then later on when it tries to log the user in using the 'old_password' it fails and returns the 'Failed Login'.
am I not defining the username it needs to login with correctly?
ive tried changing the password manualy using PHP myadmin and this:
update BWmembers set password = sha1('$new_password') where username = '$username'
replacing the passwords and username and it worked fine. dont know if that helps...?
any help would be apreciated.
Thanks.