简体   繁体   English

传递PHPSESSID会话cookie时,Guzzle挂起Apache

[英]Guzzle hangs Apache when passing PHPSESSID session cookie

I am using Guzzle get request in one PHP script to fetch another. 我在一个PHP脚本中使用Guzzle get请求来获取另一个。

$guzzle->get("urltomysecondpage")->getBody();

However, Guzzle is not aware of the session ID cookie, so my target script cannot access session variables. 但是,Guzzle不知道会话ID cookie,因此我的目标脚本无法访问会话变量。

Documentation states that all I have to do is set "cookies" to "true" and Guzzle shall use the same cookie set that the calling script sees. 文档指出,我要做的只是将“ cookies”设置为“ true”,并且Guzzle将使用调用脚本看到的相同cookie集。

$guzzle->get("urltomysecondpage", ['cookies' => TRUE])->getBody();

However, this simply doesn't work. 但是,这根本行不通。 The target script sees no cookies. 目标脚本看不到cookie。 Documentation also allows to explicitly set an array of cookies like this: 文档还允许显式设置Cookie数组,如下所示:

$guzzle->get("urltomysecondpage", ['cookies' => ['PHPSESSID'=>$_COOKIE['PHPSESSID']]])->getBody();

It works, but not if I specify one of the cookies to have the key "PHPSESSID". 它有效,但是如果我指定其中一个cookie具有键“ PHPSESSID”,则无效。 If I do, the Apache server hangs completely. 如果这样做,Apache服务器将完全挂起。 Not only the current request, but it also stops responding to all requests. 不仅当前请求,而且它也停止响应所有请求。

What's going on? 这是怎么回事?

I had some trouble using the cookies => true option. 使用cookies => true选项时遇到一些麻烦。 have you tried providing a default CooieJar ? 您是否尝试提供默认的CooieJar

Example: 例:

$jar = new GuzzleHttp\Cookie\CookieJar();
$default = [
    "cookies" => $jar
];
$c = new GuzzleHttp\Client(["defaults" => $default]);

The same cookie jar is now set by default to each request made by that client. 现在,默认情况下,该客户端的每个请求都设置了相同的Cookie罐。

The webserver isn't able to open the same session for 2 requests. 网络服务器无法为2个请求打开相同的会话。 To fix this you can first close the session and then reopen it after you've made the request with Guzzle. 要解决此问题,您可以先关闭会话,然后在通过Guzzle提出请求后重新打开该会话。

Example: 例:

session_start();

$sessionId = session_id();

session_write_close();

$client = new Client(
    array(
        'base_uri' => 'http://www.yoursite.tld',
        'cookies' => false,
    )
);

$cookie = new GuzzleHttp\Cookie\SetCookie();
$cookie->setName('PHPSESSID');
$cookie->setValue($sessionId);
$cookie->setDomain('www.yoursite.tld');

$cookieJar = new CookieJar(
    false,
    array(
        $cookie,
    )
);

$response = $client->request(
    'GET',
    '/urltomysecondpage',
    array(
        'cookies' => $cookieJar,
    )
);

session_start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重定向时 PHP 在 PHPSESSID cookie 中丢失会话 ID - PHP losing Session ID in PHPSESSID cookie when redirects 将会话cookie重命名为其他内容,而不是PHPSESSID - Rename session cookie to something else, than PHPSESSID 尽管session.cookie_secure为0,PHPSESSID仅在通过HTTPS提供服务时显示/工作 - PHPSESSID only showing/working when served through HTTPS, despite session.cookie_secure being 0 为什么即使cookie和PHPSESSID仍然存在,PHP会话信息也无法通过浏览器重新启动而保持不变? - Why is PHP session info not persisting through browser restarts, even when the cookie and PHPSESSID persists? $ _SESSION ['session_id']总是等于$ _COOKIE ['PHPSESSID']吗? - Is $_SESSION['session_id'] always equal to $_COOKIE['PHPSESSID']? 通过从cookie读取的PHPSESSID分配和读取用户的会话 - Assign and read session of user by PHPSESSID read from cookie $ _SESSION已创建,但$ _SERVER ['HTTP_COOKIE']中没有PHPSESSID - $_SESSION created but theres no PHPSESSID in $_SERVER['HTTP_COOKIE'] PHPSESSID Cookie 自动创建,无需调用 session_start() - PHPSESSID Cookie automatically created without calling session_start() PHP session_id()在每个请求上刷新,$ _COOKIE ['PHPSESSID']为空 - PHP session_id() refresh on every request, $_COOKIE['PHPSESSID'] empty PHP SESSION仅通过HTTPS起作用+ PHPSESSID cookie不发送 - PHP SESSION works only over HTTPS + PHPSESSID cookie is not sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM