par
yuuzhantar » 06 mai 2008, 19:33
Bonjour
j'ai un petit problème avec cURL pour maintenir une session ouverte
j'arrive a envoyer des requêtes get et post avec succès pour me connecter à un espace membre
mais je n'arrive pas à continuer à naviguer dans l'espace membre, ma session est fermée automatiquement
a priori celà ne vient pas du site car j'ai essayé sur deux sites différents qui utilisent des cookie pour envoyer l'identifiant de session et j'ai bien le cookie sur l'hébergement
voici mon code :
function requete_get($url,$cookie='r',$referer='')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, header_http());
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
if($cookie=='w')
{
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
}
elseif($cookie=='r')
{
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
//curl_setopt($ch, CURLOPT_USERPWD, 'user:mdp');
$ret = curl_exec($ch);
if ($ret === FALSE)
{
echo '<br>ERREUR CURL : '.curl_error();
return false;
}
else
{
curl_close($ch);
return $ret;
}
}
function requete_post($url,$array,$cookie='r',$referer='')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array);
//curl_setopt($ch, CURLOPT_HTTPHEADER, header_http());
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
if($cookie=='w')
{
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
}
elseif($cookie=='r')
{
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
$ret = curl_exec($ch);
if ($ret === FALSE)
{
echo '<br>ERREUR CURL : '.curl_error();
return false;
}
else
{
curl_close($ch);
return $ret;
}
}
/********************************************
fonction qui ouvre un fichier selon le mode choisit et créé un pointeur
********************************************/
function fichier_ouvrir($fichier_nom,$mode)
{
if(($fichier_pointeur=@fopen($fichier_nom,$mode))!=false)
{
return $fichier_pointeur;
}
else
{
return false;
}
}
/********************************************
fonction qui ferme un pointeur
********************************************/
function fichier_fermer($fichier_pointeur)
{
if(($fermeture=@fclose($fichier_pointeur))!=false)
{
return true;
}
else
{
return false;
}
}
/********************************************
fonction qui lit les lignes du fichier à partir de la position du pointeur
et retourne un tableau : une case pour chaque ligne
********************************************/
function fichier_lire($fichier_pointeur)
{
$retour=array();
while(!feof($fichier_pointeur))
{
if(($tmp=@fgets($fichier_pointeur))!=false)
{
$retour[]=$tmp;
}
}
return $retour;
}
function lire_cookie()
{
$fich=fichier_ouvrir('cookie.txt','r');
if($fich==false)
{
echo'<br>erreur ouverture cookie';
}
else
{
$cookie=fichier_lire($fich);
fichier_fermer($fich);
echo'<br>---- cookie ----<br>';
while (list($clee, $valeur) = each($cookie))
{
echo'<br>::: '.$valeur;
}
}
}
if(verification_page('xxxxxxx'))
{
$data=requete_get('xxxxxxx');
preg_replace('`\<head\>.*\</head\>`','',$data);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data.'</textarea>';
lire_cookie();
$data2=requete_post('xxxxxxx/index.php?page=connect2',
array('mail'=>'xxxxxxx',
'pass'=>'xxxxxxx'),'w','xxxxxxx/index.php?page=accueil');
preg_replace('`<head>.*</head>`','',$data2);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data2.'</textarea>';
lire_cookie();
$data3=requete_get('xxxxxxx/index.php?page=moncompte','r');
preg_replace('`\<head\>.*\</head\>`','',$data3);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data3.'</textarea>';
lire_cookie();
if(preg_match('`/index\.php\?page=deconnect`',$data3))
{
echo'<br>connecté';
}
else
{
echo'<br>déconnecté';
}
}
et voici le résultat affiché à l'écran :
voyez vous quelque chose qui ne va pas dans mon code ?
personnellement j'y suis depuis hier midi et je ne trouve pas
merci
Bonjour
j'ai un petit problème avec cURL pour maintenir une session ouverte
j'arrive a envoyer des requêtes get et post avec succès pour me connecter à un espace membre
mais je n'arrive pas à continuer à naviguer dans l'espace membre, ma session est fermée automatiquement
a priori celà ne vient pas du site car j'ai essayé sur deux sites différents qui utilisent des cookie pour envoyer l'identifiant de session et j'ai bien le cookie sur l'hébergement
voici mon code :
[php]
function requete_get($url,$cookie='r',$referer='')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, header_http());
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
if($cookie=='w')
{
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
}
elseif($cookie=='r')
{
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
//curl_setopt($ch, CURLOPT_USERPWD, 'user:mdp');
$ret = curl_exec($ch);
if ($ret === FALSE)
{
echo '<br>ERREUR CURL : '.curl_error();
return false;
}
else
{
curl_close($ch);
return $ret;
}
}
[/php]
[php]
function requete_post($url,$array,$cookie='r',$referer='')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$array);
//curl_setopt($ch, CURLOPT_HTTPHEADER, header_http());
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
if($cookie=='w')
{
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
}
elseif($cookie=='r')
{
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
$ret = curl_exec($ch);
if ($ret === FALSE)
{
echo '<br>ERREUR CURL : '.curl_error();
return false;
}
else
{
curl_close($ch);
return $ret;
}
}
[/php]
[php]
/********************************************
fonction qui ouvre un fichier selon le mode choisit et créé un pointeur
********************************************/
function fichier_ouvrir($fichier_nom,$mode)
{
if(($fichier_pointeur=@fopen($fichier_nom,$mode))!=false)
{
return $fichier_pointeur;
}
else
{
return false;
}
}
/********************************************
fonction qui ferme un pointeur
********************************************/
function fichier_fermer($fichier_pointeur)
{
if(($fermeture=@fclose($fichier_pointeur))!=false)
{
return true;
}
else
{
return false;
}
}
/********************************************
fonction qui lit les lignes du fichier à partir de la position du pointeur
et retourne un tableau : une case pour chaque ligne
********************************************/
function fichier_lire($fichier_pointeur)
{
$retour=array();
while(!feof($fichier_pointeur))
{
if(($tmp=@fgets($fichier_pointeur))!=false)
{
$retour[]=$tmp;
}
}
return $retour;
}
[/php]
[php]
function lire_cookie()
{
$fich=fichier_ouvrir('cookie.txt','r');
if($fich==false)
{
echo'<br>erreur ouverture cookie';
}
else
{
$cookie=fichier_lire($fich);
fichier_fermer($fich);
echo'<br>---- cookie ----<br>';
while (list($clee, $valeur) = each($cookie))
{
echo'<br>::: '.$valeur;
}
}
}
[/php]
[php]
if(verification_page('xxxxxxx'))
{
$data=requete_get('xxxxxxx');
preg_replace('`\<head\>.*\</head\>`','',$data);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data.'</textarea>';
lire_cookie();
$data2=requete_post('xxxxxxx/index.php?page=connect2',
array('mail'=>'xxxxxxx',
'pass'=>'xxxxxxx'),'w','xxxxxxx/index.php?page=accueil');
preg_replace('`<head>.*</head>`','',$data2);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data2.'</textarea>';
lire_cookie();
$data3=requete_get('xxxxxxx/index.php?page=moncompte','r');
preg_replace('`\<head\>.*\</head\>`','',$data3);
echo'<hr><br><textarea style="height:100px; width:100%">'.$data3.'</textarea>';
lire_cookie();
if(preg_match('`/index\.php\?page=deconnect`',$data3))
{
echo'<br>connecté';
}
else
{
echo'<br>déconnecté';
}
}
[/php]
et voici le résultat affiché à l'écran :
[quote]
---- cookie ----
::: # Netscape HTTP Cookie File
::: # http://www.netscape.com/newsref/std/cookie_spec.html
::: # This file was generated by libcurl! Edit at your own risk.
:::
::: www.xxx.com FALSE / FALSE 0 PHPSESSID 4fef5b69365f46caf10508466b8fea13
---- cookie ----
::: # Netscape HTTP Cookie File
::: # http://www.netscape.com/newsref/std/cookie_spec.html
::: # This file was generated by libcurl! Edit at your own risk.
:::
::: www.xxxxxx.com FALSE / FALSE 0 PHPSESSID 57f8db16076f5e59ddd53f6c6dc94d3d
---- cookie ----
::: # Netscape HTTP Cookie File
::: # http://www.netscape.com/newsref/std/cookie_spec.html
::: # This file was generated by libcurl! Edit at your own risk.
:::
::: www.xxx.com FALSE / FALSE 0 PHPSESSID 57f8db16076f5e59ddd53f6c6dc94d3d
déconnecté
[/quote]
voyez vous quelque chose qui ne va pas dans mon code ?
personnellement j'y suis depuis hier midi et je ne trouve pas
merci