多个cURL请求上的$ _SESSION变量为空

时间:2015-03-04 02:00:21

标签: php session curl

在我的第一个cURL请求中,我上传了一个文件,并设置了一个带有名称,扩展名等的$ _SESSION变量。在我的第二个cURL请求中,我想将上传的文件从tmp文件夹移动到用户文件夹但是很糟糕的是$ _SESSION变量是空的。为什么?

第一个请求代码代码如下所示:

$upload = curl_init();
curl_setopt($upload, CURLOPT_URL, "http://localhost/upload/" );
curl_setopt($upload, CURLOPT_POST, true );
curl_setopt($upload, CURLOPT_RETURNTRANSFER, true );
curl_setopt($upload, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
curl_setopt($upload, CURLOPT_HTTPHEADER, array("Set-Cookie: data=" . urldecode($cookie) ));
curl_custom_postfields($upload, $fields, $files);

$res =  curl_exec($upload);
curl_close($upload);

和第二个请求代码:在第一个请求之后:

$submit = curl_init();
curl_setopt($submit, CURLOPT_URL, "http://localhost/" );
curl_setopt($submit, CURLOPT_RETURNTRANSFER, true );
curl_setopt($submit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"] );
curl_setopt($submit, CURLOPT_POST, count($fields));
curl_setopt($submit, CURLOPT_POSTFIELDS, $fields_string );
curl_setopt($submit, CURLOPT_HEADER, true); 
curl_setopt($submit, CURLOPT_HTTPHEADER, array("Cookie: data=" . urldecode($cookie) ));

$res =  curl_exec($submit);
curl_close($submit);

有没有让会话保持活动的选项?当我开始在AJAX中使用javascript时,我遇到了与AJAX请求相遇的问题。

我的回复标题:

    HTTP/1.1 200 OK
Date: Wed, 04 Mar 2015 02:24:22 GMT
Server: Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0
X-Powered-By: PHP/5.3.0
Set-Cookie: PHPSESSID=g7hc328ij8lr63mps6ub44gat2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 22
Content-Type: text/html

1 个答案:

答案 0 :(得分:1)

我认为您需要一个cookie jar来跟踪您的会话:

curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookies");