简体   繁体   中英

PHP/cURL how to get Request Cookies

Hy,

I have a bit of a problem here. One client of mine asked to create a login script for a website built in ASP. As much as I can take and use Response Cookies, I am not able to take and use Request Cookies. I tried, but they are not saved in my cookie files. As such, I cannot login into the very website. Its address is http://www.itraceuk.co.uk/Default.asp?secure=signin

Here is the code I am currently using:

$ch4 = curl_init(); 
curl_setopt ($ch4, CURLOPT_URL, $posturl); 
curl_setopt($ch4, CURLOPT_HEADER, true);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt ($ch4, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt ($ch4, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36"); 
curl_setopt ($ch4, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch4, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt ($ch4, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch4, CURLOPT_COOKIESESSION, 1);
curl_setopt ($ch4, CURLOPT_COOKIEJAR, $cookie2); 
curl_setopt ($ch4, CURLOPT_COOKIEFILE, $cookie2); 
curl_setopt ($ch4, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin'); 
$result4 = curl_exec ($ch4); 
curl_close($ch4);
<?php

$posturl = 'http://www.itraceuk.co.uk/Default.asp?secure=signin';

$cookie = __DIR__ . '/cookie.txt'; // file and folder must be writeable

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

curl_setopt($ch, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');

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

var_dump($result);

var_dump(file_get_contents($cookie));

Set cookie folder/file permissions to writeable by PHP and Server.

Reuse the cookie data after the login.

Curl Post to the login form with correct credentials, that logs you in and sets the cookie (curl CURLOPT_POSTFIELDS). Then reuse this cookie for the second curl request (this code here).

For a complete example of a HTTPS cURL login, please see: https://stackoverflow.com/a/10307956/1163786

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