How To Set HTTP Response Header in PHP

Exception handling is very important for developer as well as for programming,in this tutorial we will described how to set custom header and messages into php response.
When we work on web application then each request have HTTP response.The HTTP response denotes the request was successful or have error,If response header contains error,Then you can know about what type error occurred with help of HTTP status code and message.

You can also check other tutorial of PHP headers,

PHP script to Set Response Headers

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*********************************************************************
     Purpose            : set header status.
     Parameters         : @$code: exception code
     @$status_message : message exception

     Returns            : app site id
     ***********************************************************************/
    static function errorResponse($code = 400, $status_message ='')  {
        switch($code)
        {
            case 7:
                $code='407' ;
                break;
            case 404:
                $status_message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
                break;
            case 500:
                $status_message = 'The server encountered an error processing your request.';
                break;
            case 60:
                $code='400';
                break;
            case '190':
                $code='412';
                $status_message = 'Token Error.';
                break;
        }
        $status_header = 'HTTP/1.1 ' . $code . ' ' . $status_message;
header( $status_header);
        
    }