Page 1 of 1
cURL and sessions
Posted: Mon Jun 21, 2004 7:47 pm
by Kutulu
I want to send a POST request to a URL which demands that you are logged in (a community) with a session.
Is there any way to do this, with cURL or anything else?
I need this to make a presentation generator

Posted: Mon Jun 21, 2004 7:49 pm
by feyd
curl can easily do it..
using CURLOPT_POST_FIELDS or something.
Posted: Tue Jun 22, 2004 5:43 am
by Weirdan
If remote server uses cookies to track session you could parse the response header for Set-Cookie: .*, store it to global variable and then use it on each request:
Code: Select all
//................
curl_setopt($cx, CURLOPT_COOKIE, $stored_cookie);
curl_exec($cx);
//..............
here is simple login using curl I used once:
Code: Select all
function login($login, $pass) {
$cx = curl_init("http://www.example.com/login.php");
$post_str = "username=$login&password=$pass&login= login ";
curl_set_options($cx,
array('returntransfer'=>true,
'post'=>true,
'postfields'=>$post_str,
// 'nobody'=>true, // does not work for some weird reason
'header'=>true
)
);
$ret = curl_exec($cx);
preg_match("/Set-Cookie: (.*);/i", $ret, $regs);
return $regs[1];
}
$stored_cookie = login('test', 'test');
Posted: Tue Jun 22, 2004 5:56 am
by redmonkey
You will need both COOKIEFILE and COOKIEJAR...
Code: Select all
curl_setopt($ch, CURLOPT_COOKIEFILE, "/home/devel/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/home/devel/cookie.txt");
Posted: Tue Jun 22, 2004 6:39 am
by Weirdan
there's no need to access the file (it's slow) for using just one cookie

Posted: Tue Jun 22, 2004 7:18 am
by redmonkey
How do you know it's only one cookie? and how do you know that one cookie is not modified between page requests?
Posted: Wed Jun 23, 2004 11:10 am
by Weirdan
I refer to default PHP session engine, where cookie containing sessid usually isn't modified between requests. Your approach is more generic, mine can be used in specific situations where speed is crucial.