Pagina 1 din 1

Pastrare sesiuni active la trimiterea datelor cu cURL

Scris: Mar Mai 29, 2018
de Stefan
Salut, vreau sa trimit date catre un formular cu cURL dar am o problema, ca sa accesez formularul trebuie sa fiu logat iar la trimiterea datelor imi arata ca sunt delogat, de ce? Si cum as putea pastra sesiunile active atunci cand trimit datele cu cURL?
Image: imgur.com/a/iWtb3LJ

Cod:

Cod: SelectaĊ£i tot

<?php
//receives string $html with html code
//returns the value of the html input with name of $name
function getInpValName($html, $name){
  $re ='no val';
  $dom = new DomDocument; 
   
  //load the html into the object
  error_reporting(~E_WARNING); // to not output warning errors
  $dom->loadHTML($html);
  $elms = $dom->getElementsByTagName('input');
  foreach($elms as $elm){
    if($elm->getAttribute('name') ==$name){
      $re = $elm->getAttribute('value');
      break;
    }
  }

  return $re;
}
 
//Test
$html = file_get_contents('http://localhost/whmcs/clientarea.php?action=changepw');

// echo getInpValName($html, 'token');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://localhost/whmcs/clientarea.php?action=changepw");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

// $str = substr(str_shuffle(str_repeat("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 5)), 0, 8);
$data = array(
	'token' => "". getInpValName($html, 'token') ."",
	'existingpw' => "Pass1*",
	'newPassword1' => "Pass2*",
	'newPassword2' => "Pass2*"
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo '<pre>';
print_r($data);
echo '<br />';

print_r($info);
echo '</pre>';
echo '<br />';
echo $output;
?>

Pastrare sesiuni active la trimiterea datelor cu cURL

Scris: Mie Mai 30, 2018
de MarPlo
La ceea ce vrei, e nevoie sa faci intai logarea tot prin cUrl, si sa folosesti optiunile CURLOPT_COOKIEJAR si CURLOPT_COOKIEFILE pentru retinere si re-transmitere sesiune de logarea.
Dupa cUrl-ul cu logarea se face urmatorul cUrl cu CURLOPT_COOKIEFILE si datele care vrei sa fie transmise ca utilizator logat.
Exemplu:

Cod: SelectaĊ£i tot

//1. cUrl for login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://domain/page_login);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['user'=>'Name', 'password'=>'Pass']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //for url with https
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/dir/cookieFileName');  //write cookie-session in dir/

ob_start();      // prevent any output
curl_exec($ch); // execute the curl command
ob_end_clean();  // stop preventing output

curl_close ($ch);
unset($ch);

//2. cUrl to send data after login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://domain/other_page);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['nm1'=>'val-1', 'data2'=>'value-2']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //for url with https
curl_setopt($ch, CURLOPT_COOKIEFILE, '/dir/cookieFileName');  //read cookie-session from dir/

$output = curl_exec($ch);
curl_close ($ch);

echo '<pre>'.htmlentities($output);
- Nu am testat codul. Poti sa gasesti pe net articole pe acest subiect la cautare: php curl cookiejar keep session.