Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查空重定向中的一些$\u GET变量_Php - Fatal编程技术网

Php 检查空重定向中的一些$\u GET变量

Php 检查空重定向中的一些$\u GET变量,php,Php,如何检查多个变量是否为空,如果都为空,如何将请求转发到新的URL 以下是我目前的代码: <?php if(empty($_GET['Email'] and $_GET['Phone+Number'])) { header('Location: /index.html'); exit; } ?> 只需确保您可以使用的函数是否为空 以下值被视为空。 “”(空字符串) 0(0作为整数) “0”(0作为字符串) 空的 假的 array()(空数组) var$var;(

如何检查多个变量是否为空,如果都为空,如何将请求转发到新的URL

以下是我目前的代码:

<?php
if(empty($_GET['Email'] and $_GET['Phone+Number'])) 
{ 
    header('Location: /index.html');
    exit; 
}
?>

只需确保您可以使用的函数是否为空

以下值被视为空。

  • “”(空字符串)
  • 0(0作为整数)
  • “0”(0作为字符串)
  • 空的
  • 假的
  • array()(空数组)
  • var$var;(已声明的变量,但类中没有值)
  • 要检查的代码

    $result = true; //assume it's all correct.
    foreach($_GET as $value) {
       $result = ($result && (!empty($value))); //if it's empty, I will be false. False & anything is always false. 
    }
    
    if($result) //everything is ok
    

    你可以做一个小函数来检查这个

    function check_empty_fields(array $fields) {
    
        $empty = false;
    
        foreach($fields as $field) {
    
            if(empty($field)) {
                $empty = true;
                break;  // Break as soon as the condition is reached.
            }
    
        }
    
        return $empty;
    
    }
    
    要使用此函数,请向其提供要检查的字段。我使用了PHP函数
    filter\u input
    作为一个很好的实践,以提供最少的输入转义

    现在您可以检查字段了

    if(!check_empty_fields($fields)) {
    
        /*
        * Set the HTTP status code: 307 in indicate it is a temporary redirect.
        * Use code: 301 for a permanent redirect.
        */
        header("Location: http://google.com", true, 307);
    
        // Remove this exit if there is anything to be done before a redirect happens.
        exit;
    
    }
    

    您可以创建一个函数来检查它,并将所有名称的列表作为该函数的参数。我刚刚尝试过这个方法,但在URL中显示localhost/site/success.php?Email=&Phone+Number=123456789时什么也没有发生。我想这是因为URL中显示了
    &Phone+Number
    。这意味着电话和号码之间用空格隔开。尝试在HTML中删除它。我不知道PHP会如何处理它。
    $fields = [
        'email' => filter_input(INPUT_GET, 'Email', FILTER_SANITIZE_STRING), 
        'phone' => filter_input(INPUT_GET, 'PhoneNumber', FILTER_SANITIZE_STRING)
    ];
    
    if(!check_empty_fields($fields)) {
    
        /*
        * Set the HTTP status code: 307 in indicate it is a temporary redirect.
        * Use code: 301 for a permanent redirect.
        */
        header("Location: http://google.com", true, 307);
    
        // Remove this exit if there is anything to be done before a redirect happens.
        exit;
    
    }