I want to compare if a get variable is a specific text. But I cannot get into the if-statemeant. The echo "invalid user" is not written.
code:
<?php
if ($_GET['status'] && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
when I check the variable status before (ando also after) the if-statemeant with:
echo $_GET['status'];
or with
print_r($_GET['status']);
the result is in both
login_fail
So the variable status is there and has "login_fail".
But "invalid user" is not written!
I tried it also with === or with ' instead " and much much more.
Use isset().
`
<?php
if (isset($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid useenter code herer";
}
`
Try this :
<?php
if (!empty($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
I think your code don't have any problem.
if your facing a problem till now you can add this line
before your if condition script.
$_GET['status'] = trim($_GET['status']);
In order to avoid errors when $_GET has nothing, use this:
if (!empty($_GET['status']) && ($_GET['status'] == "login_fail")) {
echo "invalid user";
}
Change to test if 'status' is set in the $_GET variable first and then check its value.
if (isset($_GET['status']) && $_GET['status'] == "login_fail")
{
echo "invalid user";
}
That way you will not run into any problems if it's not set.
You should use isset() function to check whether a variable has any value:
if(isset($_GET['status']) && $_GET['status'] == 'login_fail')
{
echo "invalid user";
}
Related
I have a php page like this
if($_GET){
if($_GET['name'] && $_GET['number']){
$name = $_GET['name'];
$number = (int) $_GET['number'];
echo "Got it!";
} else {
echo "Please give input!";
}
} else {
echo "Please give input!";
}
Now it works fine when there is no input is given. But if i give one input like page.php?name=Something&number=0 it gives error Undefined index: number
And the value of number also can be 0.
This error doesn't look good. Now how can I get rid from this? Please help!
use isset to check
also check it explicitly like $_GET['name']!='' and dont print or store whatever you get from get and post etc.. if you are storing to database using mysli_real_escape_string. and if you are outputting to browser use htmlspecialchars
if(isset($_GET)){
if(isset($_GET['name']) && $_GET['name']!='' && isset($_GET['number']) && $_GET['number']!=''){
$name = $_GET['name'];
$number = (int) $_GET['number'];
echo "Got it!";
} else {
echo "Please give input!";
}
} else {
echo "Please give input!";
I receive the information with this form correctly.
$findNIF = User::where('nif','=',$nif)->get();
$findEmail = User::where('email','=',$email)->get();
I need to compare if it is the same value the "nif" and "email" of the $findNIF and $findEmail.
$findNIF["0"]["nif"];
$findNIF["0"]["email"];
$findEmail["0"]["nif"];
$findEmail["0"]["email"];
I use the following sentence but I always receive "OK"
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}
Here
if ($findNIF["0"]["nif"] && $findNIF["0"]["email"] && $findEmail["0"]["nif"] && $findEmail["0"]["email"]){
echo "OK";
} else {
echo "NO";
}
in your condition if you get values in both of your $findNIF and $findEmail variables then it always return true.
If you want to compare the nif of $findNIF & $findEmail and email of $findNIF & $findEmail both then this would be like this
if (($findNIF["0"]["nif"] == $findEmail["0"]["nif"]) && ($findNIF["0"]["email"] == $findEmail["0"]["email"])){
echo "OK";
} else {
echo "NO";
}
Please elaborate more about what you need.
search.php
session_start();
if(isset($_GET['sessionStart'])){
echo "<br>you start a session";
$_SESSION['session']='yes';
} else {
$_SESSION['session']='no';
}
result.php
session_start();
$sen=$_SESSION['session'];
echo $sen1;
if($sen='no'){
echo $sen2;
echo '<br>Back to search page';
} else {
echo $sen3;
echo '<br>Back to search page';
}
As you can c, if !isset($_GET['sesssionStart']) sen1 and sen3 will be echo,
bu sen3 will echo 'yes'
can anyone help me solvethis ?
Thank you
This is link for search.php
start a session
You use if($sen='no'){this means you set $sen as no. Use == instead of a single = for a comparisson
So:
if($sen=='no'){
This is because instead of comparison you are using assignment in the line
if($sen='no'){ // assigning value 'no' to $sen
Change this line to
if($sen=='no'){ // comparison for equality
if($sen='no'){ to if($sen=='no'){
result.php
session_start();
//also check is required for good code writing
if(isset($_SESSION))
{
$sen=$_SESSION['session'];
}
echo $sen1;
if($sen == 'no'){ // use == in plave of =
echo $sen2;
echo '<br>Back to search page';
}else
{
echo $sen3;
echo '<br>Back to search page';
}
You're using an assignment operator (=) instead of a comparison operator (==) in your if condition. Modify that line to
if($sen=='no')
From the comment of answer owner:
You have to validate if the session has been set or not
if(isset($_SESSION['session']) && $sen == 'no'){
very short simple quiz app. browser keeps returning Correct as a response, even though it is not correct.
<?php
$answer = (isset($_POST['answer']));`
if ($answer == "Dagny Taggart")`
{
echo "Correct";
} else {
echo "wrong";
}
?>
Try this. You're currently setting $answer to isset($_POST['answer'], meaning as long as $_POST['answer'] is set, you're $answer is going to be TRUE. I would check if it's set and then set it to the $_POST value.
<?php
if(isset($_POST['answer'])) {
$answer = $_POST['answer'];
if ($answer == "Dagny Taggart")
{
echo "Correct";
} else {
echo "wrong";
}
} else {
// Do something?
}
?>
isset returns a boolean (true or false) value. So your test will always fail because true != 'Dagny Taggart'
Add this ternary and it will work
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';
I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>