url verification using get_headers function

get_headers function fetch the headers sent by server, response to the HTTP request.
Returns values in index associative array or failure.

How its works for url verification
get_headers function accept parameters, first parameter for target url and second one for array keys.

<?php
$strUrl = "http://www.google.com";
$result = get_headers($strUrl);
print_r($result);
?>

Output will be :

Array
(
[0] => HTTP/1.0 302 Found
[1] => Cache-Control: private
[2] => Content-Type: text/html; charset=UTF-8
[3] => Location: http://www.google.co.in/?gfe_rd=cr&ei=WhxtU5erAuGJ8QeO_oHgBA
[4] => Content-Length: 261
[5] => Date: Fri, 09 May 2014 18:20:10 GMT
[6] => Server: GFE/2.0
[7] => Alternate-Protocol: 80:quic
[8] => HTTP/1.0 200 OK
[9] => Date: Fri, 09 May 2014 18:20:10 GMT
[10] => Expires: -1
[11] => Cache-Control: private, max-age=0
[12] => Content-Type: text/html; charset=ISO-8859-1
[13] => Set-Cookie: PREF=ID=844418fdc97fffdc:FF=0:TM=1399659610:LM=1399659610:S=UIQuFZG5v-aDr2VH; expires=Sun, 08-May-2016 18:20:10 GMT; path=/; domain=.google.co.in
[14] => Set-Cookie: NID=67=JCxYpD9Cu_PSTfmen3N06IzXckP0uQH6x7kHCR2EiXbqn3RHkyx-H4eDK7coNOzEaidPA6Yj_Ls5ueRVvIW1_AEhB84qiUnRa0ZEDoh9y3h-M-0tKl07mLWSDP0yfP6P; expires=Sat, 08-Nov-2014 18:20:10 GMT; path=/; domain=.google.co.in; HttpOnly
[15] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
[16] => Server: gws
[17] => X-XSS-Protection: 1; mode=block
[18] => X-Frame-Options: SAMEORIGIN
[19] => Alternate-Protocol: 80:quic
)

We will pass value in format parameter for same url.

<?php
$strUrl = "http://www.google.com";
$result = get_headers($strUrl,1);
print_r($result);
?>

Output will be associative array

Array
(
[0] => HTTP/1.0 302 Found
[Cache-Control] => Array
(
[0] => private
[1] => private, max-age=0
)

[Content-Type] => Array
(
[0] => text/html; charset=UTF-8
[1] => text/html; charset=ISO-8859-1
)

[Location] => http://www.google.co.in/?gfe_rd=cr&ei=rRxtU_aUH5CQiAeOjID4Dg
[Content-Length] => 261
[Date] => Array
(
[0] => Fri, 09 May 2014 18:21:33 GMT
[1] => Fri, 09 May 2014 18:21:34 GMT
)

[Server] => Array
(
[0] => GFE/2.0
[1] => gws
)

[Alternate-Protocol] => Array
(
[0] => 80:quic
[1] => 80:quic
)

[1] => HTTP/1.0 200 OK
[Expires] => -1
[Set-Cookie] => Array
(
[0] => PREF=ID=4f33ba6f9772155a:FF=0:TM=1399659694:LM=1399659694:S=iNP92lalQ6zVXnx3; expires=Sun, 08-May-2016 18:21:34 GMT; path=/; domain=.google.co.in
[1] => NID=67=j3Ji_OLL91dDHGw0yEi_i5O2G8Lb8IMCCDJEVZ1P_abM90HGnQLs5wF7zlcUOLGe0jF3k6sp7inNTXSyfXIK3QU4avk26bNJ691oMfj9OfBO8Zi_kXgFJBmGtM-k8nkJ; expires=Sat, 08-Nov-2014 18:21:34 GMT; path=/; domain=.google.co.in; HttpOnly
)

[P3P] => CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
[X-XSS-Protection] => 1; mode=block
[X-Frame-Options] => SAMEORIGIN
)

If you closely see the array element between first and second output.
You can see the first element return “HTTP/1.0 302 Found” value and Location key is available in second output.

Status code show url redirection but we cannot verified the url is valid or not, to overcome this one. We can check on the location key which was moved from first to second url and verified it.

Now let’s see the url verification code.

<?php
$strUrl = "http://www.google.com";
$result = get_headers($strUrl,1);
?>

We are checking location key from the array list
if its found we are again sending the location value to get_headers function.

<?php
if(isset($result["Location"])) {
$result = get_headers($result["Location"],1);
}
print_r($result);
?>

Output will be :

Array
(
[0] => HTTP/1.0 200 OK
[Date] => Fri, 09 May 2014 18:38:10 GMT
[Expires] => -1
[Cache-Control] => private, max-age=0
[Content-Type] => text/html; charset=ISO-8859-1
[Set-Cookie] => Array
(
[0] => PREF=ID=03c967df448fd301:FF=0:TM=1399660690:LM=1399660690:S=--BxAA02PLcvic5k; expires=Sun, 08-May-2016 18:38:10 GMT; path=/; domain=.google.co.in
[1] => NID=67=ZEsW7J_GiMNLqJixgqZWlkSaPq8ULdwPh8j2BvjSgllKipaRSf-p68NtgsknlTPVMw2lYe50BocyWgDqw_qjfGoR-3txDyOFVMxgbatHEDgAJ5IQRFac3yL8T5hpDKhl; expires=Sat, 08-Nov-2014 18:38:10 GMT; path=/; domain=.google.co.in; HttpOnly
)

[P3P] => CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
[Server] => gws
[X-XSS-Protection] => 1; mode=block
[X-Frame-Options] => SAMEORIGIN
[Alternate-Protocol] => 80:quic
)

Now you can see the status code “HTTP/1.0 200 OK” which mean above url is verified.