Jump to content

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
https://forums.phpfreaks.com/topic/260710-cookies-and-curl/
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
https://forums.phpfreaks.com/topic/260710-cookies-and-curl/#findComment-1336332
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:

echo $_SERVER['HTTP_USER_AGENT'];

Link to comment
https://forums.phpfreaks.com/topic/260710-cookies-and-curl/#findComment-1336545
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.