简体   繁体   中英

Login to page with CURL

I have the problem with login to the website using CURL. I read the session ID but to use it, I have to reload the page, which is associated with the change. How can I dynamically use the current session 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;
?>

Greetings

You need to use CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE for this.

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

This will keep all the cookies in cookie.txt and while making a new request cookies will be taken from same cookie.txt file. This way you'll be able to maintain the session on successive HTTP requests.

如果要保持登录状态,请在登录后执行的所有curl请求中使用相同的cookie文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM