Level 1
someones ?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
i have a little website, and i want try to connect with curl
for login
$url = "https://www.mywebsite/login.php";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_URL, $url);
$timeout = 30;
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch2, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt ($ch2, CURLOPT_POST, 1);
curl_setopt ($ch2,CURLOPT_POSTFIELDS,"xxxx");
$result = curl_exec($ch2);
for take header and cookie PHPSESSID
$ch = curl_init('https://www.mywebsite/apercu.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
// get cookie
// multi-cookie variant contributed by @Combuster in comments
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
//var_dump($cookies['PHPSESSID']);exit;
And after i want recover data
$ch = curl_init('https://www.mywebsite/apercu.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Retrieving session ID
$strCookie = 'PHPSESSID=' . $cookies['PHPSESSID'] . '; path=/';
//We pass the sessionid of the browser within the curl request
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
//We receive the answer as if we were the browser
$curl_response = curl_exec($ch);
//OPTIONAL - Redirect to another page after login
$url = "https://www.mywebsite/apercu.php";
curl_setopt ($ch2, CURLOPT_POST, 0);
curl_setopt($ch2, CURLOPT_URL, $url);
$result = curl_exec($ch2);
//end OPTIONAL
curl_close($ch2);
echo $result;
but that dont work, its my first curl login, so maybe i fail with cookie ? how send the cookie ?
Thanks
Please or to participate in this conversation.