11 Replies - 1599 Views - Last Post: 08 February 2013 - 04:30 PM Rate Topic: -----

#1 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Php: Password not match and Password is empty

Posted 06 February 2013 - 08:33 PM

Hi!!! I am a beginner in PHP and we are ask to create a fields with passwords.

The code for password fields is:
<input type="password" name="Password1">
<input type="password" name="Password2">

As you can see there are two passwords, it is to verify if the password does match or not...
I get the concept and i use if-else statement but if the user does click the submit button it should print a phrase "Password is empty", if the user inputs the password but they do differ when the user input, it should print a phrase "Password not match", but if the user has correctly input both the passwords field then it should print the users password.

Here is my code:
<?php 
		if ($_POST['Password1'] != $_POST['Password2'])
			echo " Password not match. ";
		else if ($_POST['Password1'] == $_POST['Password2'])
			echo $_POST['Password1']; 
		else if (isset ($_POST['Password1']))
			echo " Password is empty. ";

?>


First one states password not match, second is to echo the user's password if they are equal, lastly to echo it if the password is not set.
I tried to run it but it functions the first and second only. the third one doesnt work!! if i dont set or input a password it doesnt show anything on the browser.. so that only means the third one is malfunction!!

Any help will be appreciated.. thank you.. :)/>

This post has been edited by e_i_pi: 06 February 2013 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Php: Password not match and Password is empty

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: Php: Password not match and Password is empty

Posted 06 February 2013 - 08:52 PM

On the last check you are checking isset($_POST['Password1']). Shouldn't you be checking !isset(...?
Was This Post Helpful? 3
  • +
  • -

#3 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 07:47 PM

View Poste_i_pi, on 06 February 2013 - 08:52 PM, said:

On the last check you are checking isset($_POST['Password1']). Shouldn't you be checking !isset(...?



even if i tried still the last one doesnt function.. if there are 3 statements then the last one doesnt function...i dont know why..ive also tried to use the ifs with the endif and it still wont function on the last statement.. :(
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 08:34 PM

isset() checks for a NULL value, not an empty string.
Was This Post Helpful? 1
  • +
  • -

#5 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,822
  • Joined: 30-April 10

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 08:46 PM

use empty()
Was This Post Helpful? 1
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,676
  • Joined: 30-January 09

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 08:48 PM

I was going to suggest that, but empty() is true if the password is '0'. Really, people shouldn't be choosing a 1 character password, but in the absence of any password strictures, '0' is still a valid string.
Was This Post Helpful? 1
  • +
  • -

#7 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,889
  • Joined: 10-May 07

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 08:51 PM

As previously suggested, you want to use the empty() function because it is passed in the form regardless if it contains a value or not, so isset() will always return true.
Was This Post Helpful? 1
  • +
  • -

#8 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,822
  • Joined: 30-April 10

Re: Php: Password not match and Password is empty

Posted 07 February 2013 - 08:54 PM

else if(empty($_POST['Password1']) && $_POST['Password1'] == ""){
   echo "Password one is empty!";
} 


Was This Post Helpful? 2
  • +
  • -

#9 brerallia  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 146
  • Joined: 21-January 13

Re: Php: Password not match and Password is empty

Posted 08 February 2013 - 01:50 AM

wow!!! thank you all for helping me with this situation.. i really appreciate it.. in return i will give a thumbs up to it.. thank you!! :)
Was This Post Helpful? 0
  • +
  • -

#10 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: Php: Password not match and Password is empty

Posted 08 February 2013 - 06:57 AM

I think braces matters, try this one
if()
{
}
else if()
{
}
else if()
{
}
Was This Post Helpful? 0
  • +
  • -

#11 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Php: Password not match and Password is empty

Posted 08 February 2013 - 04:23 PM

@Kirstine, the curly-brackets are not required for single line blocks. You can leave them out. It's more or less a matter of personal preference. - Though it tends to be less problematic to use the curly-brackets for single-liners as well.

View Postlaytonsdad, on 08 February 2013 - 03:54 AM, said:

else if(empty($_POST['Password1']) && $_POST['Password1'] == ""){
   echo "Password one is empty!";
} 


This could potentially bring up a "Undefined index" notice. If $_POST["Password1"] is not set, the first condition will evaluate as True, and the second condition will go on to try to access that unset element to compare it to the empty string, thus producing the notice.

A better way - if allowing edge cases like "0" is important enough to even consider (which I wouldn't agree with) - would be to use !isset() instead of empty there:
else if (!isset($_POST["Password1"]) || $_POST["Password"] == "") {
    echo "Password one is empty!";
}


Now it will only move on to the empty check if the element is set. And seeing as all POST fields are strings when they arrive, this is pretty much the same thing as using empty(), except allowing for "0".
Was This Post Helpful? 1
  • +
  • -

#12 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,822
  • Joined: 30-April 10

Re: Php: Password not match and Password is empty

Posted 08 February 2013 - 04:30 PM

Thank you Atli for correcting me. I would have missed that.

This post has been edited by laytonsdad: 08 February 2013 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1