PHP cURL JSESSIONID Cookie错误

时间:2012-05-11 23:06:22

标签: php cookies curl session-cookies

我不想用我的php脚本登录ssl网站。 如果我使用帖子登录表单加载页面,我会得到一个值为

的会话cookie
JSESSIONID=E180962D04FFCCDCEFF3ACA063347C23; Path=/qisserver; Secure

页面的源代码显示表单post post:

中的会话ID
<form method="post" action="https://foo.bar/baz;jsessionid=E180962D04FFCCDCEFF3ACA063347C23?state=user&amp;type=1&amp;category=auth.login&amp;startpage=portal.vm" name="loginform">

如果我登录,我会获得一个包含位置的新会话cookie:

JSESSIONID=91E63CBCE0E309D5ACE2F609453E0D63; Path=/qisserver; Secure
Location=https://foo.bar/baz;jsessionid=91E63CBCE0E309D5ACE2F609453E0D63?state=user&type=0&category=menu.browse&startpage=portal.vm

到目前为止,这是我的网络浏览器中的过程。 现在我写了一个php / curl脚本,它首先连接,收集第一个cookie并提取jsessionid值,因为它是表单动作url的一部分。

其次我的脚本应该登录并存储新的cookie,但是我收到错误。我不能告诉你更多关于错误的信息。

这是我的php:

<?php


$username = "foo";
$password = "blubb";

# Define the target and referer web pages
$target = "https://foo.bar/rds?state=user&type=0";
$ref    = "https://foo.bar/rds?state=user&type=0";


$useragent = "User-Agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0"; 


// GET JSESSION ID COOKIE START   //////////   //////////   //////////   //////////   //////////   //////////   //////////   //////////

$ch = curl_init();

    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");   // Cookie management.
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 

    curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, FALSE); // remove body 

    curl_setopt($ch, CURLOPT_TIMEOUT, 20);    // Timeout sek
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_URL, $target);             // Target site
    curl_setopt($ch, CURLOPT_REFERER, $ref);            // Referer value
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);           // Minimize logs
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);             // Limit redirections to four
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string

    # Create return array
    $header   = curl_exec($ch); 


    # Close PHP/CURL handle
    curl_close($ch);
    unset($ch);
    $cookie = findSessionID($header);


function findSessionID($html){
    //extract session id value
    $begin = 'JSESSIONID=';

    $starnr = strpos($html,$begin);
    $starnr += strlen($begin);

    $laenge = 32;
    $id= substr($html,$starnr,$laenge); 
    return $id;
}
// GET JSESSION ID COOKIE END   //////////   //////////   //////////   //////////   //////////   //////////   //////////   //////////


sleep(2);


// LOGIN START  //////////   //////////   //////////   //////////   //////////   //////////   //////////   //////////

$formurl ="https://foo.bar/qisserver/rds?;jsessionid=".$cookie."&state=user&amp;type=1&amp;category=auth.login&amp;startpage=portal.vm";
$query_string = "username=".$username."&submit=%A0Ok%A0&password=".$password;

echo"<br> Rufe URL:   ". $formurl ." auf <br> mit Querystring:   ". $query_string ."<br>";

    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
    curl_setopt ($ch, CURLOPT_POST, TRUE); 
    curl_setopt ($ch, CURLOPT_HTTPGET, FALSE); 

    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");   
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIESESSION, FALSE); 

    curl_setopt($ch, CURLOPT_TIMEOUT, 20);   
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);   
    curl_setopt($ch, CURLOPT_URL, $formurl);          
    curl_setopt($ch, CURLOPT_REFERER, $ref);         
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);        
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);          
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

    curl_setopt($ch, CURLOPT_HEADER, TRUE);   // Include head as needed
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    # Create return array
    $html   = curl_exec($ch); 

    curl_close($ch);
    echo $html;

// LOGIN END  //////////   //////////   //////////   //////////   //////////   //////////   //////////   //////////


?>

非常感谢

0 个答案:

没有答案