Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:15:42+00:00 2026-05-28T05:15:42+00:00

I have the following code and when i try to get a value from

  • 0

I have the following code and when i try to get a value from the array $headers nothing appears, but when I use var_dump($headers) it shows all the array values. What Am I doing wrong?

function linkcheck($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $headers = explode("\n", curl_exec($ch));
    curl_close($ch);

    switch ($headers[18]) {
        case "Location: https://somewebsite.com/welcome":
            echo "Actice";
            break;
        case "Location: https://somewebsite.com/no_such_link":
            echo "Inactive";
            break;

    }
}

echo linkcheck('http://somewebsite.com/54sdf');

Output

array(37) {
  [0]=>
  string(19) "HTTP/1.1 302 Found
"
  [1]=>
  string(39) "Content-Type: text/html; charset=utf-8
"
  [2]=>
  string(18) "Connection: close
"
  [3]=>
  string(12) "Status: 302
"
  [4]=>
  string(60) "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.10
"
  [5]=>
  string(34) "X-UA-Compatible: IE=Edge,chrome=1
"
  [6]=>
  string(47) "Location: https://somewebsite.com/54sdf
"
  [7]=>
  string(20) "X-Runtime: 0.006921
"
  [8]=>
  string(132) "Set-Cookie: _sp_session_id=; domain=.superpoints.com; path=/; expires=Mon, 16-Jan-2012 18:08:29 GMT
"
  [9]=>
  string(24) "Cache-Control: no-cache
"
  [10]=>
  string(69) "Server: nginx/0.7.65 + Phusion Passenger 2.2.10 (mod_rails/mod_rack)
"
  [11]=>
  string(1) "
"
  [12]=>
  string(19) "HTTP/1.1 302 Found
"
  [13]=>
  string(39) "Content-Type: text/html; charset=utf-8
"
  [14]=>
  string(18) "Connection: close
"
  [15]=>
  string(12) "Status: 302
"
  [16]=>
  string(60) "X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.10
"
  [17]=>
  string(34) "X-UA-Compatible: IE=Edge,chrome=1
"
  [18]=>
  string(55) "Location: https://somewebsite.com/no_such_link
"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:15:43+00:00Added an answer on May 28, 2026 at 5:15 am

    You are relying on a certain header being at a certain position, which it will not always be, and you are also not acounting for the fact the HTTP header keys are case-insensitive.

    Try this:

    function linkcheck($url) {
    
        // Do cURL
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $response = explode("\n", curl_exec($ch));
        curl_close($ch);
    
        // Seperate headers from body
        $parts = explode("\r\n\r\n", $response);
    
        // Turn headers into associative array
        $head = explode("\r\n", $parts[0]);
        array_shift($head); // Skip response line
        $headers = array();
        foreach ($head as $header) {
            $header = explode(':', $header);
            $key = strtolower(trim(array_shift($header)));
            $val = trim(implode(':', $header));
            if (isset($headers[$key])) {
                if (is_array($headers[$key])) {
                    $headers[$key][] = $val;
                } else {
                    $headers[$key] = array($headers[$key], $val);
                }
            } else {
                $headers[$key] = $val;
            }
        }
    
        // If there is no location header, we can't test it
        if (!isset($headers['location'])) {
            echo "No location header";
            return;
        }
    
        switch ($headers['location']) {
            case "https://somewebsite.com/welcome":
                echo "Active";
                break;
            case "https://somewebsite.com/no_such_link":
                echo "Inactive";
                break;
            default:
                echo "Unknown value";
                break;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which does nothing but reading some values from a
Dear Expert i have the following problem when i try to get the value
We have following code: try { // some code throwing MyException } catch (MyException
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code in Visual Studio 2005. Dim OutFile As System.IO.StreamWriter Try
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have the following error when I try to compile my code in g+
If in my code I have the following snippet: try { doSomething(); } catch
I am using the following code, upload.php, to try to have someone upload a
Having the following code, how can I have get receive a map, and return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.