Simulate cookie data during PHP script execution? (set language before get_headers)

I apologize in advance, as I’m sure I’m going to explain this situation horribly, since most of this is new to me.

TL;DR: I have a PHP script that retrieves the HTTP headers for a list of domains (using get_headers), but recently the owner of the domains setup language detection which has now somewhat broken the script. Instead of following the HTTP header path and stopping, it now just keeps repeating the last HTTP Location header until it times out, since the script isn’t accepting the language cookie to stop get_headers from running.

The script looks for the Location headers and would eventually return something like this after traversing the HTTP headers.

http://example_com -> httpS://example_com -> https://WWW.example_com

And then it would stop running and move on to the next domain.

However now the following information is sent by the domains in order to set the language in a cookie

Set-Cookie: LANGUAGE=en;Path=/;Domain=.example.com;Secure
Set-Cookie: DEFLANG=en;Path=/;Domain=.example.com;Secure

and if this information isn’t used the last HTTP Location header request (in this example, https://www.example.com), just keeps repeating. This is happening because the language isn’t getting set so the domain doesn’t know where to redirect the script.

I was able to find a semi-workaround, where the script, after traversing all of the HTTP headers, uses all of the Location headers but then cuts off when they start to repeat. However, this means I still need to run get_headers until it times out, which is the issue I’m now trying to solve, as the script runs for much, much longer than it used to, and I’m constantly getting excessive resource usage warnings from my web server.

So my question is, is there any way to set/simulate this cookie information

Set-Cookie: LANGUAGE=en;Path=/;Domain=.example.com;Secure
Set-Cookie: DEFLANG=en;Path=/;Domain=.example.com;Secure

before I run get_headers, so that the domain will see the language as English and act accordingly, instead of continually looping on the last HTTP Location header?

Nobody has any suggestions on how to solve this?

Bump

see http://php.net/get-headers, second example.

1 Like

Thanks @Dormilich, that sent me down the right path and now everything is working perfectly!

For anyone else trying to do the same, this is what I did.

Instead of using get_headers in the default way, like so:

$url = 'http://' . $domain;
$headers = get_headers($url, 1);

I first used stream_context_set_default to set some of the data that would be set by the cookie:

stream_context_set_default(
        array(
            'http' => array(
                'method' => "HEAD",
                'header' => "Accept-language: en\r\n" .
                            "Cookie: LANGUAGE=en;DEFLANG=en;"
            )
        )
    );

$headers = get_headers('http://' . $domain);

And now the get_headers command is only returning a few redirects and is no longer looping over and over, since it now has the language information to work with.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.