如何检查多个变量以查看它们是否为空,如果它们都为空,请将请求转发到新的URL?
到目前为止,这是我的代码:
<?php
if(empty($_GET['Email'] and $_GET['Phone+Number']))
{
header('Location: /index.html');
exit;
}
?>
我的网址看起来很喜欢这个:
localhost/site/success.php?Email=joe%40sample.com&Phone+Number=123456789
答案 0 :(得分:0)
请确保空是您可以使用的功能。
以下值被视为空。
检查代码
$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
答案 1 :(得分:0)
您可以制作一个可以检查此功能的小功能。
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_input
作为一个很好的做法,提供最少的输入转义。
$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;
}