Https Code Status
1. First of all we have to:
  1. deactivate the SSL verification because there are cases in which the site is redirected from a domain where the SSL has expired!
  2. limit the number of redirects, so as not to enter an endless loop
  3. initialize User-Agent with a real value , otherwise there is the possibility that the server will not return correct results or even deny access
$context = stream_context_create( [
	'ssl' => [
                 'verify_peer_name' => false,
		'verify_peer' => false,
	],
	'http'=>array(
		'max_redirects'=>5,
		'method'=>"GET",
		'header'=>"Accept-language: en\r\n" .
		"Cookie: \r\n" .  
		"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4\r\n" 
	)
]);
2. The next step is to initiate a request to the desired link / site , using get_headers php function:
$url = 'https://google.ca/';
$headers = get_headers($url, 1, $context);

the $headers content is similar to the following example

Array
(
    [0] => HTTP/1.0 301 Moved Permanently
    [Location] => https://www.google.com/
    [Content-Type] => Array
        (
            [0] => text/html; charset=UTF-8
            [1] => text/html; charset=UTF-8
        )

    [Date] => Array
        (
            [0] => Thu, 31 Dec 2020 00:35:26 GMT
            [1] => Thu, 31 Dec 2020 00:35:26 GMT
        )

    [Expires] => Array
        (
            [0] => Sat, 30 Jan 2021 00:35:26 GMT
            [1] => Thu, 31 Dec 2020 00:35:26 GMT
        )

    [Cache-Control] => Array
        (
            [0] => public, max-age=2592000
            [1] => private
        )

    [Server] => Array
        (
            [0] => gws
            [1] => gws
        )

    [Content-Length] => 220
    [X-XSS-Protection] => Array
        (
            [0] => 0
            [1] => 0
        )

    [X-Frame-Options] => Array
        (
            [0] => SAMEORIGIN
            [1] => SAMEORIGIN
        )

    [Alt-Svc] => Array
        (
            [0] => h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
            [1] => h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
        )

    [1] => HTTP/1.0 200 OK
    [P3P] => CP="This is not a P3P policy! See g.co/p3phelp for more info."
    [Set-Cookie] => NID=206=jJeripkadas2Nujn3ZBmKviU_COcMwsp1HWgOLvykRa1BmcrHRcEiDMXpqfdPs6se34t43OfoDy3n24sEkh-vsn7x3UqZTXc-ZS8yGNKfTZgfbngfh6L4yRwc5EkgszRta6KfhdIol_moyabBMzhbABBTJtg65pf_mkfw; expires=Fri, 02-Jul-2021 00:35:26 GMT; path=/; domain=.google.com; HttpOnly
    [Accept-Ranges] => none
    [Vary] => Accept-Encoding
)
https://www.google.com/
3. Only then can we find the http status code we are looking , from response header:
if(preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#", $headers[0] , $result ))
	$reponse_code = intval($result[1]);
else 
	$reponse_code = 0;

For those who want to test online without writing any more lines of code, you can use a tool designed exactly for this: HTTP Status Code from Header

That was all,

Happy Coding!

Tags: , , , ,

Leave a Reply

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