Jump to content

How to keep CURL session alive?


beyzad

Recommended Posts

Hi.

 

I need to read some contents from a CURL session. Then post data again with the same session. is there anyway to do that?

 

I mean somethings like Fake Yahoo! ID makers that shows the CAPTCHA to user, then submit form.

 

Thanks

Link to comment
Share on other sites

This won't work? I believe I've done this before...

<?php
//Create the curl session
$curl_handle=curl_init();

//Go to site 1
curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$site1 = curl_exec($curl_handle);

//Now go to site 2
curl_setopt($curl_handle,CURLOPT_URL,'http://www.asdf.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$site2 = curl_exec($curl_handle);

//Now close
curl_close($curl_handle);

Link to comment
Share on other sites

I assume you're logging into a website first and then trying to retrieve some information? You'll need to specify a COOKIEJAR so that any cookies created by the first request (i.e. the session id) can be stored and used in the second request.

 

And no, you don't make two calls to curl_init().

Link to comment
Share on other sites

I fount this on php.net:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, 'http://site.com/page1.php');
$result1 = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL, 'http://site.com/page2.php');
$result2 = curl_exec($ch);

curl_close($ch);
?>

 

But i still cant understand how to grab somethings from the first URL, then post them to the second url?

 

Please help me.

Link to comment
Share on other sites

To get something from the first page to use in the second request, you'll need to extract the information from the first request. You have the source of the page stored in $result1, so you could use regular expressions to get the data you need (e.g. preg_match).

 

Without actually knowing what sort of information you're trying to retrieve, it's hard to give you any more specific help than that.

Link to comment
Share on other sites

I'm not overly convinced on why you'd want to do this...but anyway.

 

You'll need to make the request to the first page, storing the cookie that is set and extracting the image name. Of course, if the image name is always the same, you don't actually need to bother, though you do still need to make the request to the first page to have the cookie set. Show the user the image. Then make the second request, providing the cookie data you saved from the first one.

Link to comment
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.