PHP有效的http状态

时间:2015-05-04 10:18:42

标签: php

在我的网站上,用户可以在标准文本输入字段中输入HTTP状态代码。即,200,400,404,403等...

有没有办法检查HTTP状态代码是否有效。例如,如果用户输入“123”,则返回false。我目前无法想到或找到一种方法,而不是做一个大丑陋的if或switch语句。

3 个答案:

答案 0 :(得分:1)

在PHP中没有内置函数可以实现这一点,但是对于检查而言,比ifswitch语句更简洁的方法可能是使用数组:

$validStatusCodes = [200, 201, 202, ...];

if (in_array($submittedStatusCode, $validStatusCodes)) {
    // Ok
} else {
    // Not ok
}

答案 1 :(得分:1)

根据w3.org,以下HTTP/1.1状态代码有效:

$status_code = array("100","101","200","201","202","203","204","205","206","300","301","302","303","304","305","306","307","400","401","402","403","404","405","406","407","408","409","410","411","412","413","414","415","416","417","500","501","502","503","504","505");

if(in_array("404", $status_code)){
    echo "valid";
  }else{
    echo "invalid";
  }

答案 2 :(得分:0)

对于使用 Symfony Laravel 的用户,您可以执行以下操作:

df[Reduce(`|`, lapply(df[-(1:2)], `>=`, 1.8)),]
#   Name Group Heath BP   PM
#1   QW  DE23  20.0 60 10.0
#2   We  Fw34   0.5 42  2.5
#4   Op  Ss14  43.0 45 96.0

假设use Illuminate\Http\Response; function is_valid_http_status(int $code) : bool { return in_array($code, array_keys(Response::$statusTexts)); } 是其中之一:

  • Response
  • Symfony\Component\HttpFoundation\Response