2015-05-04 77 views
0

在我的網站上,用戶可以在標準文本輸入字段中輸入HTTP狀態代碼。即200,400,404,403等等......PHP有效的http狀態

有沒有什麼辦法檢查HTTP狀態碼是否有效。例如,如果用戶輸入'123',它將返回false。我目前無法想象或找到一種方法,而不是做一個大的醜陋的if或switch語句。

回答

1

有沒有內置函數在PHP來實現這一點,但一查比ifswitch語句更簡潔的方式可能是利用數組:

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

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

在http://php.net/manual/en/function.http-response-code.php#107261你可以找到與大多數狀態碼的開關 –

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"; 
    }