PHP CURL 使用 preg_match 解析多个 set-cookie 标头

发布于 2024-11-16 04:53:59 字数 811 浏览 2 评论 0原文

我正在使用 PHP/CURL 来自动执行两个紧密相连的代码点火器之间的调用。

代码点火器返回两个 set-cookie 标头,一个用于包含真实会话数据的安全 cookie,一个用于与空会话的不安全连接...

Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure

我一直在尝试解析安全 cookie (两个站点都在同一个域上,所以如果我通过 CURL 获取更新的会话信息,我应该更新客户端 cookie,就像他们直接进行调用一样)

我当前正在使用以下内容来解析 cookie:

preg_match('/Set-Cookie: (.*)\b/', $Head, $Cookies);

这给了我 $Cookies

Array
(
    [0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
    [1] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
)

但这是仅匹配第一个 set-cookie 标头。我的正则表达式技能很差 - 如何匹配第二个标头?

I'm using PHP/CURL to automate calls between 2 closely tied code igniter.

Code igniter is returning two set-cookie headers, one for a secure cookie with the real session data, one for insecure connections with an empty session...

Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure

I've been trying to parse the secure cookie (both sites are on the same domain so if I get updated session information via CURL, I should update the clients cookie as if they made the call directly)

I'm currently using the following to parse the cookie:

preg_match('/Set-Cookie: (.*)\b/', $Head, $Cookies);

which gives me in $Cookies:

Array
(
    [0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
    [1] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
)

but this is only matching the first set-cookie header. My regex skills are poor - how can I match the second header?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-11-23 04:53:59

假设 $Head 是包含所有 cookie 标头的单个字符串,您正在寻找 preg_match_all()preg_match() 在找到第一个匹配项后停止。

使用 preg_match_all(),匹配的整个字符串将位于 $Cookies[0] 中。您的子模式匹配将位于 $Cookies[1] 中。

$Head = <<<HEAD
Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
HEAD;

preg_match_all('/Set-Cookie: (.*)\b/', $Head, $Cookies);

print_r($Cookies);

另外,默认

Array
(
    [0] => Array
        (
            [0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
            [1] => Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
        )

    [1] => Array
        (
            [0] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
            [1] => overshare=BdHJPVt...STsCxnMBj; path=/; secure
        )

)

情况下,通配符 (.*) 是贪婪的,因此如果标头不在不同的行上,它可能会一起消耗两个字符串。如果是这样,请尝试 (.*?) 使其变得不贪婪。

Assuming $Head is a single string containing all of the cookie headers, you're looking for preg_match_all(). preg_match() stops after finding the first match.

With preg_match_all(), matched entire strings will be in $Cookies[0]. Your subpattern matches will be in $Cookies[1].

$Head = <<<HEAD
Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path=/
Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
HEAD;

preg_match_all('/Set-Cookie: (.*)\b/', $Head, $Cookies);

print_r($Cookies);

yields

Array
(
    [0] => Array
        (
            [0] => Set-Cookie: overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
            [1] => Set-Cookie: overshare=BdHJPVt...STsCxnMBj; path=/; secure
        )

    [1] => Array
        (
            [0] => overshare=a%3A0%3A%7B%7D; expires=Thu, 17-Jun-2010 05:09:32 GMT; path
            [1] => overshare=BdHJPVt...STsCxnMBj; path=/; secure
        )

)

Also, your wildcard (.*) is greedy by default, so it may consume both strings together if the headers aren't on separate lines. If so, try (.*?) to make it ungreedy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文