Sadiqur Rahman

Archive for the ‘header’ Category

Checking the server response code is mainly a diagnostic  tool in case you are getting unexpected results when trying to view a page.  The  response code is sent by the server before the page contents along with other header information like response code, server software, cookies, content type, date etc..

For example, a user had a non existing page which was  showing his site fine but the server was returning a missing page code (404) so  none of the content of this page is actual content you are looking for.

How to send 404 response to the user?
it is very simple.. you have to use header() function to accomplish this.
<?php
header(“HTTP/1.0 404 Not Found”);
?>
Other necessary response code…

header("Location: /foo.php",TRUE,301);// 301 Moved Permanently
header("Location: /foo.php",TRUE,302);// 302 Found
header("Location: /foo.php");
header("Location: /foo.php",TRUE,303);// 303 See Other
header("Location: /foo.php",TRUE,307);// 307 Temporary Redirect

For more information on header function please keep you eye at http://us2.php.net/header

Anyway, let me expose to my original post..  I was talking on retriving response code.  It can be done by many ways including opening socket and using cURL.
I prefer to use cURL in this case as curl is faster than fsockopen.
Here is a function to retrieve http response header.

function get_http_header($url)
      {
          ob_start();
          $ch = curl_init($url);
          curl_setopt($ch, CURLOPT_HEADER, 1);
          curl_setopt($ch, CURLOPT_NOBODY, 1);
          $ok = curl_exec($ch);
          curl_close($ch);
          $head = ob_get_contents();
          ob_end_clean();
          return $head;
      }

Using this function, you will get all header information including cookies.
Look on the Sample Output of this function

HTTP/1.0 200 OK
Cache-Control: private, max-age=0
Date: Sun, 11 Jan 2009 06:34:42 GMT
Expires: -1
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=90215632c8490688:TM=1231655682:
LM=1231655682:S=aYh2YUY8dmUGmQh7;
expires=Tue, 11-Jan-2011 06:34:42 GMT; path=/; domain=.google.com.bd
Server: gws
Content-Length: 0
X-Cache: MISS from mail
Connection: close

Here we need only first line to get response code that is HTTP/1.0 200 OK
So, we can use $head=split(“\n”,$head);  to separate first line.
So, our final function will look like

function get_http_response_code($url)
      {
          ob_start();
          $ch = curl_init($url);
          curl_setopt($ch, CURLOPT_HEADER, 1);
          curl_setopt($ch, CURLOPT_NOBODY, 1);
          $ok = curl_exec($ch);
          curl_close($ch);
          $head = ob_get_contents();
          ob_end_clean();
          $head=split("\n",$head);
          $head=split(" ",$head[0]);
          return $head[1];
      }

What to Next?
I am showing you all http response code and meaning in a php array.

$response_status=array(    100=>"Continue",
            101=>"Switching Protocol",
            200=>"OK",
            201=>"Created",
            202=>"Accepted",
            203=>"Non-Authoritative Information",
            204=>"No Content",
            205=>"Reset Content",
            206=>"Partial Content",
            300=>"Multiple Choices",
            301=>"Moved Permanently",
            302=>"Found",
            303=>"See Other",
            304=>"Not Modified",
            305=>"Use Proxy",
            306=>"Switch Proxy",
            307=>"Temporary Redirect",
            400=>"Bad Request",
            401=>"Unauthorized",
            402=>"Payment Required",
            403=>"Forbidden",
            404=>"Not Found",
            405=>"Method Not Allowed",
            406=>"Not Acceptable",
            407=>"Proxy Authentication Required",
            408=>"Request Timeout",
            409=>"Conflict",
            410=>"Gone",
            411=>"Length Required",
            412=>"Precondition Failed",
            413=>"Request Entity Too Large",
            414=>"Request-URI Too Long",
            415=>"Unsupported Media Type",
            416=>"Requested Range Not Satisfiable",
            417=>"Expectation Failed",
            500=>"Internal Server Error",
            501=>"Not Implemented",
            502=>"Bad Gateway",
            503=>"Service Unavailable",
            504=>"Gateway Timeout",
            505=>"HTTP Version Not Supported",
            102=>"Processing",
            207=>"Multi-Status",
            418=>"I'm a Teapot",
            422=>"Unprocessable Entity",
            423=>"Locked",
            424=>"Failed Dependency",
            425=>"Unordered Collection",
            426=>"Upgrade Required",
            449=>"Retry With",
            450=>"Blocked",
            506=>"Variant Also Negotiates",
            507=>"Insufficient Storage",
            509=>"Bandwidth Limit Exceeded",
            510=>"Not Extended");

You can use this array along with above function.

Advertisements
Tags: , ,

My Qualification

My Classes on PHPClasses.org

Archives

December 2017
M T W T F S S
« Jan    
 123
45678910
11121314151617
18192021222324
25262728293031

Flickr Photos

Blog - Sadiqur Rahman

test