Another PHP if/else not working as expected - php

I haven't been able to find a solution to this issue and hope that a another set of eyes may pick up on something. The else $confirm = line is not executed when the first "if" is not true and I can't figure out why.
if($_POST['zip'] && $_POST['country']=='USA' && !empty($fax)){
$plus4 = substr($_POST["zip"],6,4);
if (empty($plus4)) {
$link = 'Zip + 4';
$msg = "Your Zip + 4 was not provided! We cannot fax your Representative without your Zip+4.<br/>Click here to find your ".$link ;
print "<p style=\"color: red;\"><b>$msg<br/><br/></b></p>";
}
if (empty($_POST['state']) || empty($_POST['zip'])) $statezip = false ; // Check for State & Zip Required
else $statezip = true ;
//if (empty($state) || empty($zip) || empty($plus4)) $statezip = false ; // Check for State & Zip Required
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($_POST['address']) && !empty($_POST['city']) && !empty($_POST['country']) && $statezip == true ; // Verify required fields
}
else $confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields

Your else statement will always return false for $confirm due to the last condition:
&& $statezip == true
You are setting that value in the if statement, so it is undefined in the else statement.

Add your curly braces to the ELSE statement. Since you use them in the defining IF, the parser is expecting them.
else {
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
}

use isset to check that variable exist,
function empty return true even if it contain 0 ,
when you are using else use brackets { too
else{do something}

Related

why do I keep getting missing link errors with my email activation

I have done an email activation, which is working but am trying to adopt the same concept for my antispam activation. It is working for some people but sometimes, I keep getting that one of the variables is not set, is just just a trial and error thing?
I also read somewhere that I should include a not empty check too after the isset() check.
<?php
include_once __DIR__.'/header2.php';
if(isset($_SESSION['u_uid'])) {
echo "<meta http-equiv='refresh' content='0;url=../header2.php?reply=mustloggedoutfirst'>";
exit();
} else {
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
} else {
$email = strip_tags($_GET['email']);
$username = strip_tags($_GET['userid']);
$id = strip_tags($_GET['id']);
$reply_registration_expirydate = strip_tags($_GET['reply_registration_expirydate']);
if (empty($_SESSION['key'])) {
$_SESSION['key'] = base64_encode(openssl_random_pseudo_bytes(32));
}
}
}
I forgot to add to my previous question that initially, I had the following code, which seems to work for most people for not for some...
if (!isset($_GET['email']) || !isset($_GET['activatetoken']) || !isset($_GET['reply_registration_expirydate'])) {
echo "<meta http-equiv='refresh' content='0;url=../index.php?reply=missinglink'>";
exit();
Update your IF-clause and remove the negation of "isset".
With "!isset($_GET['email']) && $_GET['email'] !== ''" you're checking if the variable is NOT set and then (being not set) if it's NOT unequal to an empty string, this must run into a "variable not set"-error.
The logical order of your if statement is probably incorrect. By "missing link errors" you probably mean this part of the if-statement is executed:
if(!isset($_GET['email']) && $_GET['email'] !== '' || !isset($_GET['userid']) && $_GET['userid'] !== '' || !isset($_GET['id']) && $_GET['id'] !== '' || !isset($_GET['reply_registration_expirydate']) && $_GET['reply_registration_expirydate'] !== '') {
formatted:
if (! isset($_GET['email']) // IS NOT SET email
&& $_GET['email'] !== '' // and IS NOT EMPTY email
|| !isset($_GET['userid']) // or IS NOT SET userid
&& $_GET['userid'] !== '' // and IS NOT EMPTY userid
|| !isset($_GET['id']) // or IS NOT SET id
&& $_GET['id'] !== '' // and IS NOT EMPTY id
|| !isset($_GET['reply_registration_expirydate']) // or IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '') { // and IS NOT EMPTY reply_registration_expirydate
Due to the precedence of the operators, your statement might evaluate to true earlier than you want. You need to use parentheses to group operands.
if ((! isset($_GET['email']) // (IS NOT SET email
&& $_GET['email'] !== '') // and IS NOT EMPTY email)
|| (!isset($_GET['userid']) // or (IS NOT SET userid
&& $_GET['userid'] !== '') // and IS NOT EMPTY userid)
|| (!isset($_GET['id']) // or (IS NOT SET id
&& $_GET['id'] !== '') // and IS NOT EMPTY id)
|| (!isset($_GET['reply_registration_expirydate']) // or (IS NOT SET reply_registration_expirydate
&& $_GET['reply_registration_expirydate'] !== '')) { // and IS NOT EMPTY reply_registration_expirydate)

PHP - An if statement with concatenated '&&' and '||' can give problems?

I have a problem with a form check that use an if statement with multiple 'and' and 'or' operators. This check return me an anomalous occasionally false value.
public function insert_checkForm($form) {
$form = array_filter($form);
if (
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
isset($form['travel_go_from']) != isset($form['travel_go_to']) ||
isset($form['work_go_from']) != isset($form['work_go_to']) ||
!isset($form['travel_go_from']) &&
!isset($form['travel_go_to']) &&
!isset($form['work_go_from']) &&
!isset($form['work_go_to'])
) {
return false;
} else {
return $form;
}
}
Last question, the above code changes compared to this (in spite of the priorities of and operators)?
[...]
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
(isset($form['travel_go_from']) != isset($form['travel_go_to'])) ||
(isset($form['work_go_from']) != isset($form['work_go_to'])) ||
(!isset($form['travel_go_from']) && !isset($form['travel_go_to']) && !isset($form['work_go_from']) && !isset($form['work_go_to']))
[...]
Thanks =)
The most common problem with isset() is that it returns false when the item is NOT SET but also returns false if the item IS SET && IS NULL.
isset($arr['nonexisting']); //this returns: false
$arr['existing'] = null;
isset($arr['existing']); //this returns: false

Looking into row for data match MySQLi

<?php
include_once("php_includes/check_login_status.php");
if($user_ok != true){
header("location: login.php");
exit();
}
?>
<?php
$result = mysqli_query($db_conx, "SELECT active FROM profilepage WHERE username='$u'") or die(mysqli_error($db_conx));
$row = mysqli_fetch_assoc($result) or die ("$u not found");
$a_check = $row['active'];
if ($u == $log_username && $user_ok == true && $a_check = 1){
echo include_once("videofeedform.php"); }
else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
{ echo "viewers should only see this"; }
exit();
if($u == $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok != true && $a_check = 0)
($a_check = 0); {
echo ' Video is going to be added here'; }
exit();
?>
ok so basically I am trying to make a module work only if it has been activated to work by the user. Only the owner of the pages sees what they is mean to and the viewer only sees what they are meant to so that part works perfect however i need to tell the script to do these IF's only if the data inside the row match 1 and if they do not match 1 (e.g 0) then they do something else. I believe the current search is only looking for a number of rows called active what is owned by the user and if it is then do...... I was wondering how I would make the query look inside the row to find the data it needs to execute as it just seems like it is just executing when it finds 1 row that matches
okay there is a decent amount wrong with this script.
you never show us what $log_username or $user_ok so I cannot help to evaluate whether those contain the correct values in your script. We'll just assume they do.
You must brush up on your usage of Comparison Operators in php.:
http://www.php.net/manual/en/language.operators.comparison.php
throughout the conditionals(if, elseif, etc..) in your script you have things like:
if ( $a_check = 1 )
but with a single '=' you are assigning the integer value 1 to the variable $a_check, this is basically the same thing as saying:
if (1)`
which evaluates to true in your if statement (any integer value other than 0 will always evaluate to true).
similarly the line
if ( $a_check = 2 )
is the same as
if (2)
and would also evaluate to true
as aldanux mentioned in his comment above, if you would like to compare if 2 variables are equal you should use ==.
if ( $var1 == $var2 )
if they are equal this will evaluate to true.
More issues with conditionals:
http://www.php.net/manual/en/control-structures.elseif.php
this block:
if ($u == $log_username && $user_ok == true && $a_check = 1){
echo include_once("videofeedform.php"); }
else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
{ echo "viewers should only see this"; }
exit();
should be more like:
if ($u == $log_username && $user_ok == true && $a_check == 1){
echo include_once("videofeedform.php");
}
else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)
{
echo "viewers should only see this";
exit(); //move exit inside the elseif block
}
fixed conditionals
use elseif since you want to evaluate another expression (e.g. $user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1),
remove semicolon from the end of your else statements
move exit() inside the elseif block (as you currently have the script would always exit on that line).
also an additional comment. the expression:
else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)
is saying i don't care is $user_ok is true or false as long as $a_check equals one. This is the same thing as doing:
else if ($a_check == 1)
though... now im thinking you just want to exit if your previous if statment false:
if ($u == $log_username && $user_ok == true && $a_check == 1) //if this fails, exit
so your code there would actually look something more like:
if ($u == $log_username && $user_ok == true && $a_check == 1){
echo include_once("videofeedform.php");
}
else{ //just remove the expression from here
echo "viewers should only see this";
exit(); //move exit inside the elseif block
}
these same issues discussed above exist with your last if statement.

How do I extract a single column from another database?

Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.

Logical operators issue

Given the below variables:
$field1;
$field2;
$field3;
$field4;
$field5;
How can I use logical operators so that the user has to fill in either field: 1,2,3 OR either field 1, 2, 4, 5. If user does not do one of the following, then I want to give error required fields not complete.
I have tried:
if ((!$field1 | !$field2 |!$field3) | (!$field1 | !$field2 |!$field4|!$field5))
$errors[] = 'You did not complete all of the required fields.';
if (!
// Exactly 1,2,3 are filled in (not 4, 5)
(!empty($field1) && !empty($field2) && !empty($field3) && empty($field4) && empty($field5))
// or Exactly 1,2,4,5 are filled in (not 3)
&& !(!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) && empty($field3))
) {
// print error
}
Let a = not empty $field1, b = not empty $field2, ..., e = not empty $field5
You want
(a && b && c) || (a && b && d && e)
= (a && b) && ( c || (d && e))
In php:
if(!empty($field1) && !empty($field2) &&
( !empty($field3) || (!empty($field4) && !empty($field5))
) {
// process
} else {
// error
}
If you consider 0 as a valid value use isset in place of empty.
Finally get to use those algebra I learned in school.
Try:
if (!empty($field1) && !empty($field2) && !empty($field3)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
Or, require that no extra fields are filled in for a given option:
if (!empty($field1) && !empty($field2) && !empty($field3) &&
empty($field4) && empty($field5)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) &&
empty($field3)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
I am sure where the $fields are comming from, but the following might do the trick:
if (!(isset($field1) && isset($field2) && isset($field3)) || !(isset($field1) && isset($field2) && isset($field3) && isset($field4)){
//Set the error
}
I occasionally do something like the following for simple validations such as this
$fields = array('field1', 'field2', 'field3', 'field4', 'field5');
foreach($fields as $field) {
if(!isset($_POST[$field]) || strlen(trim($_POST[$field])) == 0) {
// set error message
}
}

Categories