簡體   English   中英

使用CURL登錄到頁面

[英]Login to page with CURL

我使用CURL登錄網站時遇到問題。 我讀取了會話ID,但要使用它,我必須重新加載與更改關聯的頁面。 如何動態使用當前會話ID?

     <?php
    $post = 'login=testowe12&ampregister=0&password=testowe12&cookie_check=1&_xfToken=&redirect='.urlencode("http://gsmx.co");

    $handle = curl_init('http://gsmx.co/logowanie/login');
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_HEADER, 1);     
    curl_setopt($handle, CURLOPT_HTTPHEADER, array(
    'Host: gsmx.co',
    'Content-Length: 93',
    'Cache-Control: max-age=0',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Origin: http://gsmx.co',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
    'Content-Type: application/x-www-form-urlencoded',
    'Referer: http://gsmx.co/login',
    'Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4'));
    //'Cookie: xf_session='.$cookie["xf_session"].'; GCSCU_770734409090_H2=C=770734409090.apps.googleusercontent.com:S=81bef9fa0c3fbdaac13c33f8b709fc73474d3562.kvhqgGncJscCDSvW.7dde:I=1403792615:X=1403879015'));
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $post);
    $output = curl_exec($handle);

    preg_match('/^Set-Cookie:\s*([^;]*)/mi', $output, $cookie);
    parse_str($cookie[1], $cookie);

    echo $cookie["xf_session"].'<br>'.$output;
?>

問候

您需要為此使用CURLOPT_COOKIEJARCURLOPT_COOKIEFILE

curl_setopt($handle, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($handle, CURLOPT_COOKIEFILE, 'cookie.txt');

這會將所有cookie保留在cookie.txt並且在發出新請求時,cookie將從同一cookie.txt文件中獲取。 這樣,您將能夠在連續的HTTP請求上維護會話。

如果要保持登錄狀態,請在登錄后執行的所有curl請求中使用相同的cookie文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM