How can I retrieve the session id with curl in PHP? I'd like to reuse the session id for multiple users. So I would login somewhere, store the session id for this particular user and when he comes back, I'd reuse the session. Note: I want the users to use their credentials only once. That's why I have to save their session id and reuse it.
$ch = curl_init($url);
curl_setopt_array($ch, array(CURLOPT_HTTPHEADER => true));
$response = curl_exec($ch);
What you need is to set CURLOPT_HEADER
as true then you can parse through the response of header.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER => true);
$response = curl_exec($ch);
Now you can parse the header, for example I am looking for PHPSessionID
preg_match('/PHPSessionID=/', $response, $matches);
if (!empty($matches[0]))
{
$cookie = strstr($result, 'PHPSessionID=');
$cookie = strstr($cookie, ';', true);
$cookie = ltrim($cookie, 'PHPSessionID=');
}
There you go, you have the session id in $cookie
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.