Hi, What is the best way to step through long code block in PHP? I have several steps that need to happen when a form is submitted and if any fails, the script should stop and return an error.
The actions are handeled by functions. Should the if/else just check for some error variable?
$e = "0";
// Check posted passwords
// look up old password for match for extra security
if($e==0)
{
$sql = "SELECT * FROM `pins` WHERE `id`='{$_SESSION['id']}' AND `group`='{$_SESSION['group']}' AND `passwd`='". sha1($_POST['oldpassword']) ."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if($result != true || $row != true) {
$e = "Unable to match old password and group.";
}
}
// check posted passwords
if($e==0 && strlen($password1) < 5 || $password1 !== $password2)
{
$e= "Sorry, the new passwords must be at least 6 characters long and must match.";
}
if($e==0 && preg_match("/[^a-zA-Z0-9_]/", $password1) == true)
{
$e = "Passwords can contain only numbers, letters or underscores (_) and must match.";
}
// reset new password with post
if($e==0)
{
$sql = "UPDATE `pins` SET `pin`='". sha1($_POST['password']) ."', lastupdate=NOW() WHERE `id`='{$_SESSION['id']}' AND `group`='{$_SESSION['group']}' LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result) !== true) {
$e = "Unable to reset member password.";
}
}
// email new password
if($e==0 && isset($row['email']) && $row['email'] != "" && isset($_POST['emailconf']) && $_POST['emailconf'] == "true")
{ // send a confirmation email
if(send_confirm_email($row['email']) !== true) {
$e = "Uable to email reminder.";
}
}
// give thanks message and logout
if($e==0) {
echo "<p>Your password has been changed and if we have an email on record, a reminder was sent.</p>";
}
else {
echo "<p>$e</p>";
}
}
else
{ // show the login form
Case switching wouldn't work here because you're always in the 'post' case right?
Thanks for best practices advise!