php類比登入 擷取SESSIONID,並則在讀取網頁的時候發送

來源:互聯網
上載者:User
方法1用curl:
一.先開啟php curl函數庫的步驟
1).去掉windows/php.ini 檔案裡;extension=php_curl.dll前面的;    /*用 echo phpinfo();查看php.ini的路徑*/
2).把php5/libeay32.dll,ssleay32.dll複製到系統目錄windows/下
3).重啟apache
二.例子
例子:


<?php
$cookie_jar = tempnam(''./tmp'',''cookie'');
$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,''http://******'');
curl_setopt($ch, CURLOPT_POST, 1);
$request = ''email_address=&password=&action='';
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//把返回來的cookie資訊儲存在$cookie_jar檔案中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
//設定返回的資料是否自動顯示
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//設定是否顯示頭資訊
curl_setopt($ch, CURLOPT_HEADER, false);
//設定是否輸出頁面內容
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_exec($ch);
curl_close($ch); //get data after login

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, ''http://*****'');
curl_setopt($ch2, CURLOPT_HEADER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);
$orders = curl_exec($ch2);
echo '''';
echo strip_tags($orders);
echo '''';
curl_close($ch2);
?>
方法2用fsockopen:

<?php
function GetWebContent($host, $method, $str, $sessid = '''')
{
    $ip = gethostbyname($host);
//echo "ip=$ip<br>";
    [email=$fp=@fsockopen($ip,80]$fp=@fsockopen($ip,80[/email]);
    if (!$fp) return;
    fputs($fp, "$method ");
    fputs($fp, "Host: $host ");
    if (!empty($sessid))
    {
        fputs($fp, "Cookie: PHPSESSID=$sessid; path=/; ");
    }
    if ( substr(trim($method),0, 4) == "POST")
    {
        fputs($fp, "Content-Length: ". strlen($str) . " "); //  別忘了指定長度
    }
    //fputs($fp, "Content-Type: application/x-www-form-urlencoded ");
fputs($fp, "Content-Type: application/x-www-form-urlencoded ");
fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1) )");//add by Ew 071012
fputs($fp, "Connection: Keep-Alive ");
    if ( substr(trim($method),0, 4) == "POST")
    {
        fputs($fp, $str." ");
    }
    while(!feof($fp))
    {
        $response .= fgets($fp);
    }
    $hlen = strpos($response," "); // LINUX下是 " "
    $header = substr($response, 0, $hlen);
//echo "header=$header<hr><hr>";
    $entity = substr($response, $hlen + 4);
    if ( preg_match(''/PHPSESSID=([0-9a-z]+);/i'', $header, $matches))
    {
        $a[''sessid''] = $matches[1];
    }
    if ( preg_match(''/Location: ([0-9a-z_?=&#.]+)/i'', $header, $matches))
    {
        $a[''location''] = $matches[1];
    }
    $a[''content''] = $entity;    
    fclose($fp);
    return $a;
}

  $response = GetWebContent("$host","POST /$login_page HTTP/1.0", $str);//登入得到新的session_id
  //...可以在這裡先儲存session_id
  $response = GetWebContent("$host","GET /$somepage HTTP/1.0", '''', $response[''sessid'']);//使用session_id訪問頁面
  echo $response[''location''].$response[''content'']."<br>";
?>

  



<
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.