Page 1 of 1
More on POST without a form (simulation
Posted: Sun Nov 16, 2003 12:39 am
by alexeiz
I already asked for help on this matter and found a related posting by hedge that was useful, but I couldn' make it to work no matter what I tried - probably I didn'r really understood it.
The I tried to use cURL functions and it was much easier. I needed two form simulations: one for signing up for mailing list and the other for login.
First worked well and here is how i did it:
$url ='
www.superpayline.com/SAFELIST/signup.php' ; ///address of the list
$postnames_signup = array ('username','password', 'contact', 'list','action'); ///array of field names
$postdata_signup = array ('alexz',PASSWORD,'
alexei@alexeiz.com','
alexei@alexeiz.com','click here to join');
MORE on the same - the message gets cut!!
Posted: Sun Nov 16, 2003 12:48 am
by alexeiz
------- As I said, this one worked well, so I set another one for login:
$postnames_login = array ('username','password','action');
$postdata_login = array ('alexz','MUDILLO','click here to login');
$url_l = '
http://www.superpayline.com/SAFELIST/members/login.php';
function sl_login ($url_l,$n,$d,$return) {
if (!$curld = curl_init()) return "Bad connection!";
curl_setopt($curld, CURLOPT_POST, true);
$post_arr_l = array ($n[0] => $d[0],$n[1] => $d[1],$n[2] => $d[2]);
print_r ($post_arr_l);
curl_setopt($curld, CURLOPT_POSTFIELDS,$post_arr_l);
curl_setopt($curld, CURLOPT_URL, $url_l);
if ($return =="true")
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
$output2 = "Returned: <hr>$output<hr\n";
curl_close($curld);
return $output2; }
echo sl_login ($url_l,$postnames_login,$postdata_login,'true');
-----This also seemed to wotk well BUT!...
The script at
http://www.superpayline.com/SAFELIST/members/login.php is written to set username and password cookies. It does set them up (I'm checking in the browser) when the page is accessed through the login form.
When accessed through cURL functions script returns the page with "Login successful" message but the cookies are NOT SET!!!
Cookies are set the simplest way:
setcookie("s_user", $username, time()+3600);
setcookie("s_pass", $password, time()+3600);
Posted: Sun Nov 16, 2003 10:38 am
by hedge
I think I know what's going on.
The cookies are not set because they are returned in the response back to you as http headers. To maintain your authentication you just need to pass them along on subsequent requests. With raw http you just pass them along with a "Cookie: " header... not sure how to do it with curl.
Posted: Sun Nov 16, 2003 12:07 pm
by alexei
Thanks hedge!
You are correct - I requested for the header in returned variable (CURLOPT_HEADER) and got back:
HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Sun, 16 Nov 2003 17:38:38 GMT Server: Apache/1.3.27 (iTools/MacOSX) mod_ssl/2.8.12 OpenSSL/0.9.6g PHP/4.3.2 mod_perl/1.26 mod_fastcgi/2.2.12 X-Powered-By: PHP/4.3.2 Set-Cookie: splistmem=alexz; expires=Sun, 16-Nov-03 18:38:38 GMT Set-Cookie: splistpass=mudillo; expires=Sun, 16-Nov-03 18:38:38 GMT Transfer-Encoding: chunked Content-Type: text/html
As I understand now I need to extract cookie information from this string and then use ether CURLOPT_COOKIE or CURLOPT_COOKIEFILE to subsequently send the. Unfortunately the explanations I got are not for ignoramuses like me. Is there a way to easily parse the header string I get back?
Posted: Sun Nov 16, 2003 1:09 pm
by hedge
First split the headers from the content at the first \r\n\r\n, then the headers are delimited with \r\n
POST without a form cURL functions!
Posted: Mon Nov 17, 2003 11:43 pm
by alexeiz
See my prevous posting.
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Tue, 18 Nov 2003 04:51:56 GMT
Server: Apache/1.3.27 (iTools/MacOSX) mod_ssl/2.8.12 OpenSSL/0.9.6g PHP/4.3.2 mod_perl/1.26 mod_fastcgi/2.2.12
X-Powered-By: PHP/4.3.2
Set-Cookie: splistmem=alexz; expires=Tue, 18-Nov-03 05:51:58 GMT
Set-Cookie: splistpass=mudillo; expires=Tue, 18-Nov-03 05:51:58 GMT
Transfer-Encoding: chunked
Content-Type: text/html
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY background="
http://www.superpayline.com/safelist/images/bg.gif">
<TABLE cellPadding=0 cellSpacing=0 width="100%" bgColor="#ffffff">
<TR>
<TD vAlign=top>
======= and so on
The whole returned page was placed by curl_exec function int $output.
I was not sure if the format of this reterned variable would be always the same, so here is what I did to extract cookies info:
$ar = explode ("\r\n\r\n",$output);
for ($i = 0; $i <= count($ar); $i++)
{
$headpart = $ar[$i];
$c_pos = strpos($headpart, "Set-Cookie");
if ($c_pos)
{
$car = explode ("\r\n",$ar[$i]);
for ($x = 0; $x <= count($car); $x++)
{
if (substr($car[$x],0,3) == "Set")
{
if (!$cc_line) $cc_line = $car[$x];
else $cc_line = "$cc_line \r\n $car[$x]";
}
}
}
}
It is not very elegant but gives the result; cookies on separate lines in $cc_line variable.
Here is what is next:
if (!$curl2 = curl_init()) return "Bad connection!";
curl_setopt($curl2, CURLOPT_POST, true);
curl_setopt($curl2, CURLOPT_POSTFIELDS,$post_arr_l);
curl_setopt($curl2, CURLOPT_URL, $url_l);
curl_setopt($curl2, CURLOPT_COOKIE, "Set-Cookie: splistmem=alexz; expires=Tue, 18-Nov-03 03:30:40 GMT");
curl_setopt($curl2, CURLOPT_COOKIEFILE, $cc_line);
output2 = curl_exec($curld);
curl_close($curl2);
It should set the cookies as if from the file www/superpayline.com/safelist/login.php but does not.
Something is wrong and I don't know why
Posted: Tue Nov 18, 2003 8:04 am
by Weirdan
Remove 'Set-Cookie:' from curl_setopt. The manual says you have to pass cookie
contents, i.e.
curl_setopt($curl2, CURLOPT_COOKIE, "splistmem=alexz; expires=Tue, 18-Nov-03 03:30:40 GMT");
BTW, the clients shouldn't use Set-Cookie header, read the
RFC2965 for detailed explanation.
Also read the [php_man]curl_setopt[/php_man] page, especially user comments.
Posted: Tue Nov 18, 2003 8:25 am
by hedge
Weirdan wrote:Remove 'Set-Cookie:' from curl_setopt. The manual says you have to pass cookie
contents, i.e.
curl_setopt($curl2, CURLOPT_COOKIE, "splistmem=alexz; expires=Tue, 18-Nov-03 03:30:40 GMT");
BTW, the clients shouldn't use Set-Cookie header, read the
RFC2965 for detailed explanation.
Also read the [php_man]curl_setopt[/php_man] page, especially user comments.
Agreed, you are receiving a 'set-cookie' header, you need to send back a 'cookie' header.
Posted: Tue Nov 18, 2003 10:37 am
by alexeiz
Thanks, that makes sense!
So, here is what I sent, one cookie just for testing:
($url_l and $post_arr_l are set as in previous examples)
if (!$curl2 = curl_init()) return "Bad connection!";
curl_setopt($curl2, CURLOPT_POST, true);
curl_setopt($curl2, CURLOPT_POSTFIELDS,$post_arr_l);
curl_setopt($curl2, CURLOPT_URL, $url_l);
curl_setopt($curl2, CURLOPT_COOKIE, "splistmem=alexz; expires=Tue, 18-Nov-03 03:30:40 GMT");
curl_close($curl2);
Posted: Tue Nov 18, 2003 10:50 am
by m3mn0n
Check your pm, alexeiz.
Posted: Tue Nov 18, 2003 11:00 am
by alexei
Do you mean the time cookies expire?
I already changed it after I posted. I take this time from the header I get back when I send username and password the first time.
Still no cookies set!

Posted: Tue Nov 18, 2003 11:36 am
by Weirdan
Once again, check the manual page for curl_setopt. There is example how to implement authenticated requests using cookies (it's your case). It uses cookijar but it doesn't make much difference, I guess.
Posted: Tue Nov 18, 2003 12:31 pm
by alexei
As I understand, COOKIEJAR option writes to a file on the disk.
Is it possible to put the info in a variable instead, considering that the initial hreader request and the second - with cookie setting - are in the same script.
I'm just trying to save time - in the end script I'll neet to submit this to hundreds of lists at a time.
COOKIEJAR WORKING, COOKIEFILE DOES NOT!
Posted: Tue Nov 18, 2003 1:58 pm
by alexeiz
Well, I guess I have to drop my attempts a programming! I read a lot and I set up two scripts, the first sends username and password, gets cookie info and writes it to cookies.txt file - it works!:
curl_setopt ($curld, CURLOPT_VERBOSE, 1);
curl_setopt ($curld, CURLOPT_HEADER,1);
curl_setopt ($curld, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curld, CURLOPT_POSTFIELDS,$post_arr_l);
curl_setopt($curld, CURLOPT_COOKIEJAR,"cookies.txt");
curl_setopt($curld, CURLOPT_URL, $url_l);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
SECOND PART - Why is it cutting my messages off????
Posted: Tue Nov 18, 2003 2:13 pm
by alexeiz
So, it wrote the info to the file, now I execute the second script:
if (!$curl2 = curl_init()) return "Bad connection!";
curl_setopt($curl2, CURLOPT_POST, true);
curl_setopt ($curl2, CURLOPT_HEADER,1);
curl_setopt ($curl2, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl2, CURLOPT_POSTFIELDS,$post_arr_l);
curl_setopt($curl2, CURLOPT_URL, $url_l);
curl_setopt($curl2, CURLOPT_COOKIEFILE, "cookies.txt");
Judging by the examples I seen and tried to understand, this should read to cookies.txt file and set them at $url_l location, but it does not!
What a jungle!