Forum Moderators: coopster

Message Too Old, No Replies

Cookie Handling with CURL SETOPT

         

ainoy31

5:46 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Hello-

I am having an issue with saving the session ID so I can use it later as I move to the next page on a site. I am able to output the session ID on screen. 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_POSTFIELDS, "__VIEWSTATE=$viewstate&ctl00\$ContentPlaceHolder1\$txtUserName=#*$!x&ctl00\ $ContentPlaceHolder1\$txtPassword=#*$!x&ctl00\$ContentPlaceHolder1\$cmdLogin= Submit&__EVENTVALIDATION=$validation");curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);

The header output is:
HTTP/1.1 200 OK
Date: Wed, 14 Nov 2007 17:43:33 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=nltmbbb4fqknx355ry4hh545; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 8579

I have tried the following but it does not seem to work.
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');

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

Thank you.

[edited by: dreamcatcher at 7:38 pm (utc) on Nov. 15, 2007]
[edit reason] Fixed side scroll. [/edit]

PHP_Chimp

7:05 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



CURLOPT_COOKIEJAR is for storing the received cookies.
CURLOPT_COOKIEFILE is for sending back cookies.

Have you tried -
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
To see if that works? Maybe a problem with dirname().
The next thing may be that php (operating an nobody) doesnt have permission to create or write to that file.
Are you getting any error messages? If so what are they?
Is the cookie.txt file being created?

Also the COOKIEJAR description is

The name of a file to save all internal cookies to when the connection closes.
and in your code the connection is not closed. So try calling curl_close($ch); and see if it works then.

ainoy31

8:16 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Thanks for the reply back. You were right. The issue was with the file permission. I figure we already had rights to create and write to it. Thank you for the help. Much appreciation.

PHP_Chimp

9:11 pm on Nov 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you havent already found a solution you may be able to get around the problem by creating a blank file called cookie.txt. So long as you set the permissions so that 'nobody' can read and write to that file (CHMOD 666, unfortunately) php will be able to use that file.
To stop the general public from reading that file, you can put it below the web root. As php will be able to access it there, but the general public wont.