php cURL oturumda korumak için nasıl?

4 Cevap php

nasıl cURL oturumda koruyabilirsiniz?

i'am having a code the sends login details of a site and logs in successfully i need to get the session maintained at the site to continue.

Burada cURL kullanarak siteye giriş için kullanılan benim kodu

  <?php  
        $socket = curl_init();
        curl_setopt($socket, CURLOPT_URL, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_REFERER, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_POST, true);
    curl_setopt($socket, CURLOPT_USERAGENT, $agent);
    curl_setopt($socket, CURLOPT_POSTFIELDS, "form_logusername=XXXXX&form_logpassword=XXXXX");
    curl_setopt($socket, CURLOPT_COOKIESESSION, true);
    curl_setopt($socket, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($socket, CURLOPT_COOKIEFILE, "cookies.txt");
    $data = curl_exec($socket);
    curl_close($socket); 
   ?>

4 Cevap

İşte ben bunu yapmak için bulunan en iyi yoldur link:

Metin körüklü blogpost içeriğinin bir 'harmanlanmıştır' versiyonu:

 $useragent = $_SERVER['HTTP_USER_AGENT'];
 $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

session_write_close();

$ch = curl_init();
$ch = curl_init($rssFeedLink); 
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );

$response = curl_exec($ch); 
curl_close($ch); 

session_write_close() ne yapar? Bu, geçerli oturum ve mağaza oturum verilerini biter. Birden fazla komut oturumu ile oynamak, bu yüzden, onu kilitler Görünüşe göre, PHP sevmez. Session_write_close koyarak bunu almak ve kullanmak böylece geçerli oturum saklandığından emin kılar.

kullanmak istemiyorsanız session_write_close() yeni bir oturum kimliği yerine geçerli oturum kimliği kullanılarak üretilecektir.

Ayrıca PHPSESSID oturum değişkeni adı ile değiştirilmesi gerekir. Göre OWSAP recommandations o anId gibi daha genel bir şey olmalıdır.

Bazen öylesine i CURLOPT_USERAGENT parametresini dahil yazı ile bir kullanıcı ajan göndermek gerekir.

Eğer bir klasöre herhangi bir başvuru olmadan cookies.txt dosyasına atıfta konum beri benim ilk tahminim yazılabilir olmayan bir klasördeki bir dosyaya yazmak için çalışıyoruz olurdu. Yani ilk aslında bir cookies.txt dosyayı bulmak kontrol ve oturum tanımlama (lar) varsa beklediğiniz.

Bu oturumlar ile Curl nasıl olduğunu

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

Ben ImpressPages Bu gördüm

Herhalde bir işlev içine bu kodu taşımak olacaktır. Başarılı bir girişten sonra, şimdi size cookie.txt dosyasında sahip çerez ile ilişkili oturumu var. Sonraki isteklerinde, sadece çerez dosyasını kullanarak tutmak ve bu sitede geçerli bir oturum olmalıdır.