Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP valid http statuses

Tags:

php

On my website users can enter a HTTP status code into a standard text input field. i.e, 200, 400, 404, 403 etc...

Is there any way to check if the HTTP status code is valid. For example if a user enters '123' it will return false. I currently cannot think of or find a way other than doing a large ugly if or switch statement.

like image 890
itsliamoco Avatar asked Sep 11 '25 10:09

itsliamoco


1 Answers

For those using Symfony or Laravel, you can do the following :

use Illuminate\Http\Response;

function is_valid_http_status(int $code) : bool {
    // thanks @James in the comments ^^
    return array_key_exists($code, Response::$statusTexts);
} 

Assuming that Response is one of those:

  • Symfony\Component\HttpFoundation\Response
  • Illuminate\Http\Response
like image 73
boehm_s Avatar answered Sep 13 '25 00:09

boehm_s