note 128650 added to function.header

From: Date: Fri, 30 Jun 2023 08:14:35 +0000
Subject: note 128650 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-220610@lists.php.net to get a copy of this message
Ways to send a status code using enums: HttpStatusCode: <?php enum HttpStatusCode: int { case Continue = 100; case SwitchingProtocols = 101; case OK = 200; case Created = 201; case Accepted = 202; case NonAuthoritativeInformation = 203; case NoContent = 204; case ResetContent = 205; case PartialContent = 206; case MultipleChoices = 300; case MovedPermanently = 301; case Found = 302; case SeeOther = 303; case NotModified = 304; case UseProxy = 305; case TemporaryRedirect = 307; case BadRequest = 400; case Unauthorized = 401; case PaymentRequired = 402; case Forbidden = 403; case NotFound = 404; case MethodNotAllowed = 405; case NotAcceptable = 406; case ProxyAuthenticationRequired = 407; case RequestTimeout = 408; case Conflict = 409; case Gone = 410; case LengthRequired = 411; case PreconditionFailed = 412; case RequestEntityTooLarge = 413; case RequestURITooLong = 414; case UnsupportedMediaType = 415; case RequestedRangeNotSatisfiable = 416; case ExpectationFailed = 417; case InternalServerError = 500; case NotImplemented = 501; case BadGateway = 502; case ServiceUnavailable = 503; case GatewayTimeout = 504; case HTTPVersionNotSupported = 505; }; ?> Getting the appropriate string: <?php private static function decodeHttpResponse( HttpStatusCode $statusCode ): string { return match ( $statusCode ) { HttpStatusCode::Continue => 'Continue', HttpStatusCode::SwitchingProtocols => 'Switching Protocols', HttpStatusCode::OK => 'OK', HttpStatusCode::Created => 'Created', HttpStatusCode::Accepted => 'Accepted', HttpStatusCode::NonAuthoritativeInformation => 'Non-Authoritative Information', HttpStatusCode::NoContent => 'No Content', HttpStatusCode::ResetContent => 'Reset Content', HttpStatusCode::PartialContent => 'Partial Content', HttpStatusCode::MultipleChoices => 'Multiple Choices', HttpStatusCode::MovedPermanently => 'Moved Permanently', HttpStatusCode::Found => 'Found', HttpStatusCode::SeeOther => 'See Other', HttpStatusCode::NotModified => 'Not Modified', HttpStatusCode::UseProxy => 'Use Proxy', HttpStatusCode::TemporaryRedirect => 'Temporary Redirect', HttpStatusCode::BadRequest => 'Bad Request', HttpStatusCode::Unauthorized => 'Unauthorized', HttpStatusCode::PaymentRequired => 'Payment Required', HttpStatusCode::Forbidden => 'Forbidden', HttpStatusCode::NotFound => 'Not Found', HttpStatusCode::MethodNotAllowed => 'Method Not Allowed', HttpStatusCode::NotAcceptable => 'Not Acceptable', HttpStatusCode::ProxyAuthenticationRequired => 'Proxy Authentication Required', HttpStatusCode::RequestTimeout => 'Request Timeout', HttpStatusCode::Conflict => 'Conflict', HttpStatusCode::Gone => 'Gone', HttpStatusCode::LengthRequired => 'Length Required', HttpStatusCode::PreconditionFailed => 'Precondition Failed', HttpStatusCode::RequestEntityTooLarge => 'Request Entity Too Large', HttpStatusCode::RequestURITooLong => 'Request-URI Too Long', HttpStatusCode::UnsupportedMediaType => 'Unsupported Media Type', HttpStatusCode::RequestedRangeNotSatisfiable => 'Requested Range Not Satisfiable', HttpStatusCode::ExpectationFailed => 'Expectation Failed', HttpStatusCode::InternalServerError => 'Internal Server Error', HttpStatusCode::NotImplemented => 'Not Implemented', HttpStatusCode::BadGateway => 'Bad Gateway', HttpStatusCode::ServiceUnavailable => 'Service Unavailable', HttpStatusCode::GatewayTimeout => 'Gateway Timeout', HttpStatusCode::HTTPVersionNotSupported => 'HTTP Version Not Supported', default => 'Unknown HTTP status code' }; } ?> and set the header with this: <?php public static function sendStatusCode( HttpStatusCode $httpStatusCode ): void { header( sprintf( 'HTTP/1.1 %d %s', $httpStatusCode->value, self::decodeHttpResponse( $httpStatusCode ) ) ); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.168 (proxied: 185.163.92.217) ---- Manual Page -- https://php.net/manual/en/function.header.php Edit -- https://main.php.net/note/edit/128650 Del: integrated -- https://main.php.net/note/delete/128650/integrated Del: useless -- https://main.php.net/note/delete/128650/useless Del: bad code -- https://main.php.net/note/delete/128650/bad+code Del: spam -- https://main.php.net/note/delete/128650/spam Del: non-english -- https://main.php.net/note/delete/128650/non-english Del: in docs -- https://main.php.net/note/delete/128650/in+docs Del: other reasons-- https://main.php.net/note/delete/128650 Reject -- https://main.php.net/note/reject/128650 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220610) next »