cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

CURL and cookies

From: Amaury Willemant <awillemant_at_yahoo.fr>
Date: Fri, 26 May 2006 18:33:23 +0000 (GMT)

Hi everyone,

I am trying to log on a website using PHP and CURL.

First of all, I copied the HTML <form> from the website and when I used it, it worked perfectly.
So, in a second step, I took every vars (visible and hidden) from the web page and built a postfield string I use in CURL.

I tested the two methods with a page that dumps $_POST and it is exactly the same result but when I test them with the website I want to access, the method using curl does not work.

I think it is because I do not manage very well the session cookies but I am not sure.

Here is my code with the two methods, sorry, some vars are in french ;)

CURL ONE :
========

<?
//GET FORM INFORMATIONS
$ch = curl_init();
$adresse='https://intrapril.april.fr';
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_URL,$adresse);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$cookie='cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
$form=curl_exec ($ch);

//GET ACTION ATTRIBUTE FROM THE FORM
$regex="!action=\"(.*)\"!isU";
$res=array();
preg_match_all($regex,$form,$res);
$action=$res[1][0];

//GET __VIEWSTATE HIDDEN VARIABLE
$regex="!name=\"__VIEWSTATE\" value=\"(.*)\"!isU";
$res=array();
preg_match_all($regex,$form,$res);
$viewstate=$res[1][0];

$adresse="https://intrapril.april.fr/IntraprilV2/UIL/Web/Partage/Cms/".$action;

$postfields="__EVENTTARGET=".
            "&__EVENTARGUMENT=".
            "&__VIEWSTATE=".urlencode($viewstate).
            "&Login1:noCorrespondant=".urlencode("<my_login>").
            "&Login1:MotDePasse=".urlencode("<my_password>").
            "&Login1:Connexion_x=10".
            "&Login1:Connexion_y=10";

curl_setopt($ch, CURLOPT_URL,$adresse);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);

curl_exec ($ch);

curl_close($ch);

AND THE HTML ONE:
==============
<form name="Login1:Form1" method="post" action="<? echo $adresse; ?>" id="Login1_Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="<? echo $viewstate ;?>" />
<input name="Login1:noCorrespondant" id="Login1_noCorrespondant" type="text" class="textbox" /><br>
<input name="Login1:MotDePasse" id="Login1_MotDePasse" type="password" class="textbox" /><br>
<input id="Checkbox" type="checkbox" value="checkbox" name="checkbox" class="checkbox">

</form>

Could you help me and explain how session cookies work in curl please ?

Thank you in advance !

Purplefrog

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2006-05-26