我使用 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)时,它不起作用。
我制作一个.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脚本不能共享会话???