PHP - conditional statement not working properly

≡放荡痞女 提交于 2019-12-12 01:56:57

问题


I'm working in PHP and JSON at API for my mobile application. I tried to write a registration module, but a part of my conditional statements don't work as expected.

If statement 1 :

if(!isset($_GET['username']) || !isset($_GET['password']) || !isset($_GET['imei']) || !isset($_GET['imie']) || !isset($_GET['nazwisko']) || !isset($_GET['email']) || !isset($_GET['zgoda']) || !isset($_GET['telefon']) || !isset($_GET['zgoda2']) || !isset($_GET['kraj']));
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 }

It should give an error, when there is an argument missing, but it is giving an error always.

My query :

username=konrad12&password=xxx&imei=000000000000000&nazwisko=Potter&imie=Ronald&email=xxx@xxx.pl&zgoda=1&telefon=000&zgoda2=1&kraj=Poland

If statement 2 :

 if(strlen($c) != 15 || !validEmail($f) || strlen($g) != 1 || strlen($i) != 1 || wez_id_kraju($j) == 0)
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 } 

It should give an error, when var values are incorrect, but it is giving an error always.

My variables :

 $z = mysql_real_escape_string($_GET['username']);
 $b = mysql_real_escape_string($_GET['password']);
 $c = mysql_real_escape_string($_GET['imei']);
 $d = mysql_real_escape_string($_GET['nazwisko']);
 $e = mysql_real_escape_string($_GET['imie']);
 $f = mysql_real_escape_string($_GET['email']);
 $g = mysql_real_escape_string($_GET['zgoda']);
 $h = mysql_real_escape_string($_GET['telefon']);
 $i = mysql_real_escape_string($_GET['zgoda2']);
 $j = mysql_real_escape_string($_GET['kraj']);

If statement 3 :

 if($g != 0 or 1 || $i != 0 or 1)
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 } 

It should give an error, when value of $g or $i isn't 1 or 0, but it is giving an error always.

Please help me, I tried a lot of things, but I can't find a solution

@Edit :

My valid email function :

    function validEmail($email)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) && !$atIndex)
       {
          $isValid = false;
       }
       else
       {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);
          if ($localLen < 1 || $localLen > 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen < 1 || $domainLen > 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if
    (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                     str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/',
                 str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }
          if ($isValid && !(checkdnsrr($domain,"MX") || 
     ↪checkdnsrr($domain,"A")))
          {
             // domain not found in DNS
             $isValid = false;
          }
// I add that text ...
      mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
      mysql_select_db(DB_BASE);
      $q = "SELECT * FROM `system_domeny`";
      $a = mysql_query($q);
      while($wynik = mysql_fetch_array($a))
      {
       if($domena == $wynik[1]) $isValid = false;
      }
// ...
       }
       return $isValid;
    }

回答1:


remove the semicolon after the last bracket of the if.

first statement ends

 || !isset($_GET['kraj']));

I think the second statement looks ok, is it an issue with the email validation function or the other function in the last check.

the last statement should be something like

if(($g != 0 && $g != 1) || ($i != 0 && $i != 1))



回答2:


This statement

if($g != 0 or 1 || $i != 0 or 1)

is definitely not when value of $g or $i isn't 1 or 0. There are various solutions, this is what you could do just using logical operators (split out over several lines and decorated with a generous helping of parentheses for readability):

if ( 
     ( ( $g != 0) && ( $g != 1) ) 
     || 
     ( ( $i != 0) && ( $i != 1) ) 
   )

Also note that or and || have different precedence which can lead to quite puzzling situations. For simplicity sake it's better to stick to || (and &&). Read this SO question for more info about the difference between || and or




回答3:


first change first if to:

 $gets = array('username', 'password', 'imei', 'imie', 'nazwisko', 'email', 'zgoda','telefon','zgoda2','kraj');

 $er = 0;
 foreach($gets as $get){
  if(!isset($_GET[$get])){
   $er++;
   $error[] = $get;
  }
 }

 if($er > 0){
  $returning = array('error' => 'Invalid query, please fill these parameters: ['.implode(", ", $error).']');
  echo json_encode($returning);
  //break is not like exit, there is no function of break inside if!
  exit;
 }

if three, as answered by others also, change it to:

 if( ($g != 0 && $g != 1) || ($i != 0 && $i != 1))
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  exit;
 }



回答4:


make your json array like this:

 $returning=array();
$str = array('error' => 'Invalid query'); 
array_push($returning,$str); 
echo "{\"response\":".json_encode($returning)."}";  

and remove terminator(;) after while condition from first statement, and try this for third statement:

if($g != 0 ||$g != 1 || $i != 0 ||$i != 1)
 {
  $returning = array('error' => 'Invalid query');
  echo json_encode($returning);
  break;
 } 


来源:https://stackoverflow.com/questions/17745452/php-conditional-statement-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!