CURL如何在远程站点注入COOKIE

时间:2011-11-27 05:23:32

标签: php cookies curl

我不能在第2页注入cookie,有些提示请。

使用这种方法我用catcha加载page1包含cookie的内容 并将其写入cookie.txt:

define('COOKIE', './cookie.txt');
define('CAPTCHA', './captcha.png');

function saveCaptcha() {
$header = array('Content-Type: image/png');
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_COOKIESESSION, true);
curl_setopt($ch,CURLOPT_COOKIEJAR, COOKIE);
curl_setopt($ch,CURLOPT_URL,'http://example.com/captcha/securimage_show.php');
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8118');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
$fileContents = curl_exec($ch);
curl_close($ch);
$newImg = imagecreatefromstring($fileContents);
return imagepng($newImg, "CAPTCHA",9);
}

$fr = filesize(COOKIE);
echo COOKIE . ': ' . $fr . ' bytes';
if ($fr <= 50) saveCaptcha();

Cookie成功保存并写入cookie.txt,现在我们以表格形式通过验证码:

<form action="index.php" method="post">
    <input type="hidden" name="p" value="login" />
    <div class="line">
        <label>Captcha</label>
        <img src="CAPTCHA" border="0" />
    </div>
    <div class="line">
        <label>&nbsp;</label>
        <input type="text" name="captcha" value=""/>                
    </div>
    <div class="line">
        <label>&nbsp;</label>
        <input type="submit" value="Login" />
    </div>
</form>  

现在我们用$ _POST提取验证码并使用我们的cookie.txt加载page2:

    //extract data from the post
    extract($_POST);

    //set POST variables
    $url = 'http://example.com/index.php';

        //open connection
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch,CURLOPT_COOKIESESSION, true);
        curl_setopt($ch,CURLOPT_PROXY, '127.0.0.1:8118');
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POSTFIELDS, 'p=login&user=myuser&pass=123qwe&captcha='.$_POST['captcha'].'&submit=Login');
        curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($ch,CURLOPT_REFERER, 'http://example.com/');

        //execute post
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);

        $fa = fopen(COOKIE, 'w'); 
        @fwrite(COOKIE,'');
        fclose($fa);

Page2已加载但没有cookie。一些提示可能你会看到这个问题((谢谢。

0 个答案:

没有答案