Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Very Basic IF statement problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Very Basic IF statement problem

Postby ShaunN11 » Fri Apr 04, 2014 5:44 am

Hello everyone, I am new to PHP and programming in general.
I am building a website and I have just implemented a feature to allow a logged in user to change their password. My current way of validating the password change is very flawed though, for example, it will allow a user to change to a blank password, providing that their old password matches with what they have provided in the form.

I am looking for a way of validating the form using IF statements, that will output an error message into a variable if they make a mistake, so that I can display the error to the user so they know what they are doing wrong.

The code I have at the moment is this:

Code: Select all
   $oldpassword = $context->postpar('oldpassword', '');
      $newpassword = $context->postpar('newpassword', '');
      $repeatpassword = $context->postpar('repeatpassword', '');
      $error = array();
      //Load user bean using email address and current password (don’t forget to hash)
      $user = $context->user();
      if ($newpassword == $repeatpassword)
         if ($user->pwd == $oldpassword)
            $success[] = 'Your details have been saved';
            $local->addval('success', $success);
            $error[] = 'Your Old Password Does Not Match, Please Try Again.';
            $local->addval('error', $error);
         $error[] = 'Your New Passwords Do Not Match, Please Try Again.';
         $local->addval('error', $error);
         return 'profile.twig';

I am using RedBeanPHP ORM, however as the problem is with simple IF statements, I was hoping I could get some help on this issue?

The problem is that obviously, this is only outputting a singular error message... So if the users new passwords didnt match, and the old password didnt match the database, it would only display 1 of these errors.

Thank you guys.
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Apr 04, 2014 5:34 am

Re: Very Basic IF statement problem

Postby seandisanti » Sat Apr 05, 2014 10:09 am

an easy way to do it, is to start with an empty array of messages to be returned, then as each check is run, if it fails, add to the array. once you're done, if the array is not empty, a failure has happened, display messages. something like:
Code: Select all

= array();

if (
$msgs[] = 'Password must be at least 8 characters';

 * other checks here
if (count($msgs)>0){
"Password validation failed:\n";

untested code but pretty straightforward
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.