background image

PHP 如何获取 Cookie 模拟登录 

一、定义

Cookie 存储路径

必须使用绝对路径

$cookie_jar = dirname(__FILE__)."/pic.cookie";
 

二、获取

Cookie

cookie 存入文件

$url = "http://1.2.3.4/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
$content = curl_exec($ch);
curl_close($ch);
 

三、模拟浏览器获取验证码

该服务器验证码有漏洞,可以自己指定

取出

cookie,一起提交给服务器,让服务器以为是浏览器打开登陆页面

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
 

四、

POST 提交