Jump to content

cookies and curl


Recommended Posts

I want to use curl on a website that requires you to be logged in. It's not a very fancy site, so I don't think it saves the cookie to only be used by a certain IP, but that shouldn't be a problem, because I can always just create a new user for use on the server.


I know the domain, path, name and content (of course also the date it expires), because this is already set in my web browser. I also know how to connect to the website and get it with curl, but I want to get data from the website that I can only get while I'm being logged in.


So basically, I want to use the data I know about the cookie to make sure I'm logged in so I'm able to get the data that only logged in users can get.


This is probably super easy, but I really couldn't find any good information from my search around on the net!


Thanks! :)

Link to comment
Share on other sites

This is kind of silly. It's no wonder I wasn't able to understand this by myself, or in the end I actually did. The problem was when I tried to write cookies to a file, so I later could use them, I did it by doing this:

curl_setopt($curl_handle, CURLOPT_COOKIEJAR, 'cookies.txt');

It does not save the cookies in a file named cookies.txt in the same folder as the script runs in. So I had to do this instead:

curl_setopt($curl_handle, CURLOPT_COOKIEJAR, realpath('.').'\cookies.txt');

Now it actually saved the cookie in the same folder as the php script was in! :)

From here I bet I can do the rest on my own haha

Will update thread when I'm finally done.

Link to comment
Share on other sites

I was able to use the search function in the forum and format the output.


Just in case anyone wonders...


To use the cookie saved earlier in the cookiejar:

curl_setopt($curl_handle, CURLOPT_COOKIEFILE, realpath('.').'\cookies.txt');


You can create a test page that sets a cookie for you to store. setcookie

By doing this, you can see how the cookiejar file is supposed to look like, and add your own cookies without having to make the webserver do so. You can find cookies currently set with Firefox here: Tools->Options->Privacy->remove individual cookies->


To send post data, I first needed to know the data that is being sent. I used the add-on Live HTTP Headers to see the header data being sent. Under where it says content-length there should be a long text string. Copy it.


$postdata = 'the string you copied';
curl_setopt($curl_handle, CURLOPT_POST, preg_match('/=/',$postdata));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postdata);


To follow redirect:

curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);


To send referer url:

curl_setopt($curl_handle, CURLOPT_REFERER, 'some url');


To send useragent data:

curl_setopt($curl_handle, CURLOPT_USERAGENT, 'some useragent / to fake netbrowser');

But to actually appear like you are being real, you need to know what a real netbrowser send of data:


Link to comment
Share on other sites


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.