为什么在此代码[PHP-CURL]中不能使用相同的cookie访问两个不同的网页?

时间:2018-12-22 17:02:53

标签: php php-curl

我正在尝试编写脚本以登录到特定站点,并在该站点中获取多个网页以生成php变量。在这里,我在两个curl请求中都使用相同的cookie。

在随附的代码中,第一个curl请求返回主页,但是第二个请求返回登录页面,而不是所请求的页面(似乎被视为新请求)。两种情况下我都使用相同的cookie文件。

<?php
$username='xxxxx';
$password='xxxxxxx';
$cookie="C:/Games/cookie21.txt";

$url = 'http://xxxx/xxx/login.php';
$postdata = "?&uname=$username&upwd=$password";

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);

$url1 = 'http:///xxxx/xxx/forms/personal_info.php';
$ch1 = curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url);
curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch1, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch1, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch1, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch1, CURLOPT_REFERER, $url1);
$result1 = curl_exec ($ch1);
echo $result1;
curl_close($ch1);
?>

有人可以解释这种奇怪行为的原因以及该代码中需要进行的修改,以便使用php-curl在站点中获取多个网页来获取php变量吗?

0 个答案:

没有答案