Jump to content

[SOLVED] Cookie Handling with CURLOPT_COOKIEFILE


ainoy31

Recommended Posts

Hello-

 

I am able log into an ASP site with an username and password.  At the same time, I need to save the ASP session ID to a file so I can use it for the next page.  My problem is that I am not able to save the session ID to my cookie.txt file.  Here is my code.

 

$url = 'http://.../.../TracingLogin.aspx'; 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__). '/cookie.txt');

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');

curl_setopt($ch, CURLOPT_POSTFIELDS, "__VIEWSTATE=$viewstate&ctl00\$ContentPlaceHolder1\$txtUserName=XXXX&ctl00\$ContentPlaceHolder1\$txtPassword=XXXX&ctl00\$ContentPlaceHolder1\$cmdLogin=Submit&__EVENTVALIDATION=$validation");

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);

echo curl_error($ch);

 

I am able to see the session ID.

 

HTTP/1.1 200 OK

Date: Wed, 14 Nov 2007 15:34:30 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

X-AspNet-Version: 2.0.50727

Set-Cookie: ASP.NET_SessionId=aziug1idyeszbj45ha3efmnq; path=/; HttpOnly

Cache-Control: private

Content-Type: text/html; charset=utf-8

Content-Length: 8579

 

Much appreciation on any suggestions.  Thank you.  AM

I figure it out.  The solution is as follows:

 

I used curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); to store the cookie variable and if I had to go to another page, I used curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); to retrieve my session ID variable.  The thing to keep in mind is that check to make sure that PHP has permission to create or write to a file.  If not, change the cookie file property to have these permissions.  Also, if using the curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'), make sure to close the curl session -> curl_close($ch).  Hope this helps someone else as it was a learning experience from me.  Thanks.  AM

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.