php模拟了session以后,其实对验证码也一样有效。其中的核心原理就是利用登陆,来读取cookie或者Session
$v){
$postfields.=urlencode($k).'='.urlencode($v).'&';
}
$params[CURLOPT_POST]=true;
$params[CURLOPT_POSTFIELDS]=$postfields;
if(isset($_COOKIE['cookie_jar']) && ($_COOKIE['cookie_jar'] || is_file($_COOKIE['cookie_jar']))){
$params[CURLOPT_COOKIEFILE]=$_COOKIE['cookie_jar'];
}else{
$cookie_jar=tempnam($cookie_path,'cookie');//产生一个cookie文件
$params[CURLOPT_COOKIEJAR]=$cookie_jar;//写入cookie信息
setcookie('cookie_jar',$cookie_jar);//保存cookie路径
}
curl_setopt_array($ch,$params);
$content=curl_exec($ch);
var_dump(strip_tags($content));
//第二步, 利用读取的cookie来请求需要Session登陆的页面
$params[CURLOPT_FOLLOWLOCATION]=true;
$nexturl='http://localhost/DEMO/SessionTest/session_index.php';
$params[CURLOPT_URL]=$nexturl;
$params[CURLOPT_POSTFIELDS]='';
curl_setopt_array($ch,$params);
$content=curl_exec($ch);
echo strip_tags($content);
curl_close($ch);
?>
/bin/php test_session.php
string(33) "Session Login Success!
"
Session! Welcome, java-er.com
我们看到成功的读取了登陆后的结果
本地文件夹出现一个文件 cookiehY0cBE
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE / FALSE 0 PHPSESSID r19bjc4ek0s1qt1o76p32b3il2
这个文件记录了session的PHPSESSID.