想通过curl模拟提交,用一套用户名、密码能够登陆所有的系统,现在不知为什么,我在第一次登陆的时候设置的cookie,在第二次登陆的时候也能够获取到这个cookie,但是就是登陆不进去,不知道是不是在浏览器中没有存入session而导致的。
这是我的代码:
<?php
extract($_POST) ;
//set POST variables
$fields_string = "<?xml version=\"1.0\"?><req><usrid>zhangyf</usrid><pass>d41d8cd98f00b204e9800998ecf8427e</pass></req>";
$ch = curl_init() ;
Session_start();
$sessionName = session_name();
$sessionId = session_id();
echo $sessionName;
echo $sessionId;
//header('Set-Cookie: session_name()= session_id(); path=/');
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.11/cores/system/login.action");
//curl_setopt($ch, CURLOPT_COOKIEJAR, "/srv/www/html/cookie.txt");
curl_setopt($ch, CURLOPT_POST,1) ;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8", "Content-length: ".strlen($fields_string)));
//execute post
$c = curl_exec($ch) ;
setcookie($sessionName,$sessionId,time()+3600*24);
//close connection
Session_destroy();
curl_close($ch) ;
分不多,希望各位大哥大姐帮下忙,整了好几天没整出来,小弟不甚感激。
这是我的代码:
<?php
extract($_POST) ;
//set POST variables
$fields_string = "<?xml version=\"1.0\"?><req><usrid>zhangyf</usrid><pass>d41d8cd98f00b204e9800998ecf8427e</pass></req>";
$ch = curl_init() ;
Session_start();
$sessionName = session_name();
$sessionId = session_id();
echo $sessionName;
echo $sessionId;
//header('Set-Cookie: session_name()= session_id(); path=/');
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "http://192.168.1.11/cores/system/login.action");
//curl_setopt($ch, CURLOPT_COOKIEJAR, "/srv/www/html/cookie.txt");
curl_setopt($ch, CURLOPT_POST,1) ;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8", "Content-length: ".strlen($fields_string)));
//execute post
$c = curl_exec($ch) ;
setcookie($sessionName,$sessionId,time()+3600*24);
//close connection
Session_destroy();
curl_close($ch) ;
分不多,希望各位大哥大姐帮下忙,整了好几天没整出来,小弟不甚感激。
一 想通过curl模拟提交,用一套用户名、密码能够登陆所有的系统,
ps:这个几乎不可能实现。(比如说有些论坛必须用中文名,这个就存在编码问题。)二 我在第一次登陆的时候设置的cookie,在第二次登陆的时候也能够获取到这个cookie,但是就是登陆不进去,
ps:没见你在第一次登陆的时候设置的cookie(curl_setopt($ch, CURLOPT_COOKIEJAR, "/srv/www/html/cookie.txt");//这个必须要起用)再次登陆时要读取cookie要用:curl_setopt($ch, CURLOPT_COOKIEFILE, "/srv/www/html/cookie.txt");
第二点,可能你没明白我的意思,我是想通过第一次的登陆,第一次登陆是指登陆http://192.168.1.11/cores/system/login.action后,会在服务端产生一个SESSION,来记录这次登陆的用户名、密码,在这个SESSION中有一个sessionid,下次登陆另一个系统的时候,通过这个sessionid就不用再登陆了,直接进入。我在第一次登陆的时候setcookie($sessionName,$sessionId,time()+3600*24)到客户端,当我下次登陆到另一个系统时,能够获取到第一次的cookie信息,但是即使获取到了,也不能进入,还得重新登陆,现在就是这儿的问题。我做的是把几个系统合到一起,其他系统都是用java写的,因此好像改不了那几个写好的系统,只能通过curl来模拟登陆。
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);