hi I am using following code to validate username and password please can any body check where is the mistake...

$adminuser = $_POST['adminuser'];
$adminpass=$_POST['adminpass'];


if((!$adminuser=="") or (!$adminpass=="")){
	echo "Please enter Username and Password";
}
elseif((!$adminuser=="admin") or (!$adminpass=="manoj")){
	echo "Invalid Username and Password";
}
else{
	"good";
}

Recommended Answers

All 14 Replies

Please wrap code in [code]

[/code] tags code highlighting makes simple errors, obvious errors

<?php 
$adminuser = $_POST['adminuser'];
$adminpass=$_POST['adminpass'];
if((!$adminuser=="") or (!$adminpass=="")){
echo "Please enter Username and Password";
}
elseif((!$adminuser=="admin") or (!$adminpass=="manoj")){
echo "Invalid Username and Password";
}
else{ "good"; } ?>

try

<?php 
$adminuser = $_POST['adminuser'];
$adminpass = $_POST['adminpass'];
if((!$adminuser) or (!$adminpass)){ echo "Please enter Username and Password"; }
elseif(($adminuser=="admin") and ($adminpass=="manoj")){ echo "good"; }
else { echo "Invalid Username and/or Password";  }  ?>

almostbob, I think we must try to use && instead of and,

anyways, i think we can also use this code...

<?php
$adminuser = $_POST['adminuser'];
$adminpass=$_POST['adminpass'];

if(($adminuser || $adminpass)==NULL){ echo "Please enter Username and Password";}
else if(($adminuser=="admin") && ($adminpass=="manoj")){echo "Good!";}
else{echo "Invalid Username and Password";}
?>
this code detect the empty field.

      if(!$adminuser|| strlen($adminuser= trim($adminuser)) == 0){
      echo "empty value";
      }

why can't you try it like this....

if(($_POST['adminuser']=="") || ($_POST['adminpass']=="")){
	echo "Please enter Username and Password";
}
elseif(($_POST['adminuser']!="admin") || ($_POST['adminpass']!="manoj")){
	echo "Invalid Username and Password";
}
else{
	"good";
}

what is the difference between your and my logic ? I just I just passed the value of $_POST to $adminuser and $_POST to adminpass nothing much...

and I have done that because, I think by this we can save coding time... you just say that, which will be good for a coder?

$_POST['adminpass']

or

$adminpass

hummmm ?

and saiprem,
just check your line no. 5, you have used || for checking that the username and password matches each other or not... just check it...

elseif(($_POST['adminuser']!="admin") || ($_POST['adminpass']!="manoj"))

Well I can bet that i can sing in if i only know password OR id... anyways you have a great idea for coding... lolz ;)

and saiprem,
just check your line no. 5, you have used || for checking that the username and password matches each other or not... just check it...

elseif(($_POST['adminuser']!="admin") || ($_POST['adminpass']!="manoj"))

Well I can bet that i can sing in if i only know password OR id... anyways you have a great idea for coding... lolz ;)

I don't think there will be no difference between (or) or ||
So I used it, and I want to know that, have you tried executing the code which I given in the post(Sorry I haven't experimented it), I hope it should work, If not I am wrong in my post.

No saiprem,

I have not tried to run your code. But I can clearly see that there will be a logical error... on above line... we must use && or and for the condition... otherwise it will only check either id or password is correct or not...

why can't you try it like this....

if(($_POST['adminuser']=="") || ($_POST['adminpass']=="")){
	echo "Please enter Username and Password";
}

hey....harsha is right. here we can use "and" operation. . but your condition
is not work here. for example i am entering adminuser field and not entering password now i am submitting the form then the above condition checks now the condition is false. it will goes to else part. look at that code once.

muralikalpana thanks for understanding me... do you know what is the main problem in this code?

lseif(($_POST['adminuser']!="admin") || ($_POST['adminpass']!="manoj")){
	echo "Invalid Username and Password";
}

This will require only one condition to be true out of two... If you enter "admin" as user id, and "harsh" as password then the condition will be true... and if you enter "harsh" as user id and "manoj" as password, then also the condition will be true...

Have a nice day.. :)

muralikalpana thanks for understanding me... do you know what is the main problem in this code?

lseif(($_POST['adminuser']!="admin") || ($_POST['adminpass']!="manoj")){
	echo "Invalid Username and Password";
}

This will require only one condition to be true out of two... If you enter "admin" as user id, and "harsh" as password then the condition will be true... and if you enter "harsh" as user id and "manoj" as password, then also the condition will be true...

Have a nice day.. :)

yes..you are absolutly correct.

hey....harsha is right. here we can use "and" operation. . but your condition
is not work here. for example i am entering adminuser field and not entering password now i am submitting the form then the above condition checks now the condition is false. it will goes to else part. look at that code once.

Yes I agree with that, the earlier code was with or, so I kept the same in LOGICAL NOTATION and used $_POST for checking.

saiprem,

Dear, we are here to learn and teach something, not to lose or win anything... am i right ?

Anyways i am leaving this thread from now... Sorry if I have done any mistakes in my past posts, and i had a question, this thread was clearly solved at second and third post, then why still this thread is not marked as solved...? anyways I learned so many things in this thread, even luckily I got a down vote also...

Okey then Bye... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.