Hooo Posted August 22, 2009 Share Posted August 22, 2009 Hi, I believe the main part of the code is correct, however the logic I believe is wrong. I know there are many misplaces { and } and would like to know what to change to make it work. Thanks. <html> <body> <?php include 'config.php'; include 'opendb.php'; session_start(); $opass = $_POST["opass"]; $npass = $_POST["npass"]; $npass1 = $_POST["npass1"]; $salt = 's+(_a*'; $salt_passo = md5($opass.$salt); $salt_passn = md5($npass.$salt); $result = mysql_query("SELECT userpass FROM Users WHERE usname = '" .$_SESSION['usname'] . "'"); if(isset($_SESSION['usname'])) { else { if ($salt_passo != $result) { echo "Your old password was entered incorrectly"; } else { if ($salt_passo != $salt_passn) { echo "The two new password didn't match"; } else { $sql=mysql_query(UPDATE Users SET userpass='$salt_passn' WHERE usname = '" .$_SESSION['usname'] . "'"); if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } } } } } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
bundyxc Posted August 22, 2009 Share Posted August 22, 2009 Honestly, your logic is so screwed up, I have no clue what you're even trying to do. Either document your code, or let us know what you're trying to do... but honestly, if a human can't understand your code, your computer won't be able to do it. Comment your lines with //Comment goes here. Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Basically, the script is to change a password once a member is logged in. So first I include the files, start the session and ask if a session is set. If it is ( { ) then the elses happen, like if old password inputted into form beforehand matches pass in database, carry on, if not, echo error basically. Then it checks that both New Password and Confirm New Password fields are matching, and carries on and does the SQL update. At the end, the if session set ends with a redirect to index, which is the login page. Hope you understand? Thanks, I know it isn't pretty either Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 Try this =) <?php include 'config.php'; include 'opendb.php'; session_start(); $opass = $_POST["opass"]; $npass = $_POST["npass"]; $npass1 = $_POST["npass1"]; $salt = 's+(_a*'; $salt_passo = md5($opass.$salt); $salt_passn = md5($npass.$salt); $result = mysql_query("SELECT userpass FROM Users WHERE usname = '{$_SESSION['usname']}'") or die(mysql_error()); if(isset($_SESSION['usname'])) { if ($salt_passo != $result) { echo "Your old password was entered incorrectly"; } elseif ($salt_passo != $salt_passn) { echo "The two new password didn't match"; } else { mysql_query("UPDATE `Users` SET `userpass`='{$salt_passn}' WHERE `usname` = '{$_SESSION['usname']}'") or die("Error: ".mysql_error()); } } else { echo '<meta http-equiv="refresh" content="2;url=index.php">'; } ?> I think you've been programming another language for a while, and newly started PHP? Instead of doing the following else{ if{ And forget to close both of them Try using the elseif() {} =) Best wishes //AngelicS Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Done what you have suggested, however now when I enter the old pass, plus a new passX2, I get: Your old password was entered incorrectly. Could this be something to do with the encrypting? I believe I have used the same algorithm of md5 + $salt Thanks Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 Try this =) $salt_passo = md5($opass.=$salt); $salt_passn = md5($npass.=$salt); Best wishes //AngelicS Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Same outcome fella. =[ Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 Check the register script, and make sure that you are using the same encryption salt =) That's my tip. Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 $salt = 's+(_a*'; $salt_pass = md5($pword.$salt); Then: $sql="INSERT INTO Users (usname, userpass, useremail, userage) VALUES ('$uname','$salt_pass','$jmail','$age')"; Appears to be the same to me Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 $result = mysql_query("SELECT userpass FROM Users WHERE usname = '{$_SESSION['usname']}'") or die(mysql_error()); if you try to echo the variable $results, I think that you wont get anything =) Am I right? If I am, try doing it like this instead: $result = mysql_query("SELECT userpass FROM Users WHERE usname = '{$_SESSION['usname']}'") or die(mysql_error()); $row=mysql_fetch_assoc($result); And then when you get to the if statement, change it to this: if ($salt_passo != $row['userpass']) { echo "Your old password was entered incorrectly"; } Best wishes //AngelicS Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Haha, think we are getting there *sigh* Now I get: The two new password didn't match I don't see anything similar wrong with that however :/ Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 You are trying to match the old and the new password. Is that what you want? Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 No, the code I have (Changed from first time I posted) elseif ($npass != $npass1) { echo "The two new password didn't match"; } and the Variables: $opass = $_POST["opass"]; $npass = $_POST["npass"]; $npass1 = $_POST["npass1"]; $salt = 's+(_a*'; $salt_passo = md5($opass.=$salt); $salt_passn = md5($npass.=$salt); Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 Try adding this row: $salt_npass1 = md5($npass1.$salt); and then change the if statement to this: if ($salt_passn != $salt_npass1) { echo "The two new password didn't match"; } Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Edited my post, sorry, noticed that mistake There is no need to check when both are encrypted as it will encrypt with the SQL anyway, so I checked npass != npass1. Both of which are taken directly from the previous form, and should both be filled with the same password. Quote Link to comment Share on other sites More sharing options...
AngelicS Posted August 23, 2009 Share Posted August 23, 2009 So is it working? Quote Link to comment Share on other sites More sharing options...
Hooo Posted August 23, 2009 Author Share Posted August 23, 2009 Isn't =.= I must be annoying you <3 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.