How to Set Custom HTTP Header Response

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.
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.

Below is simple code example:

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);
        
    }
The following two tabs change content below.
Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Latest posts by Parvez (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>