我使用curl登录网站。代码是:
<?php
$cookie_file = tempnam('./temp','cookie');
$login_url = 'http://bbs.xxxx.com/login.php';
$post_fields = 'cktime=31536000&step=2&pwuser=xx&pwpwd=111111';
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
$url='http://bbs.xxx.com/userpay.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
preg_match("/<li> (.*)<\/li>/",$contents,$arr);
echo $arr[1];
curl_close($ch);
?>
它工作正常,但问题是,当我把它变成两个文件(a.php,b.php)时。它不起作用。
我做a.php就像:
<?php
$cookie_file = tempnam('./temp','cookie');
$login_url = 'http://bbs.xxxx.com/login.php';
$post_fields = 'cktime=31536000&step=2&pwuser=xx&pwpwd=111111';
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
?>
和b.php喜欢:
<?php
$cookie_file = tempnam('./temp','cookie');
$url='http://bbs.xxx.com/userpay.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
preg_match("/<li> (.*)<\/li>/",$contents,$arr);
echo $arr[1];
curl_close($ch);
?>
我首先访问a.php,然后访问b.php。它不起作用!!!! 谁能帮我? 它不同的curl脚本不能共享会话???
答案 0 :(得分:0)
在第一个CODE中,您为两个请求使用一个$ cookie_file 你应该在a.php和b.php中使用相同的$ cookie_file 但是你用
创建了两个不同的$ cookie_file$cookie_file = tempnam('./temp','cookie');
你在a.php中创建一个 然后在b.php中创建另一个不同的。