Stopbyte

Doing HTTP GET Request and Passing Cookies Received from Previous Web Request Using PHP!

Is there is an easy way to get cookies from an HTTP web request and store them, and then re-send those cookies again using another http web request?

i need to do this in PhP!

thanks in advance!

I believe cURL is the best and easiest way to go for that.

here is an example of how you can Get cookies from first http web request and use those cookies in the second request:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.YourRequestWebsite.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
// Here we store cookies sent from the server in th file "cookies.log"
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies.log');
 
/* You can do your cookies processing and manipulation if needed in that file content. */
 
/* and Here we "cookies.log" will be the source of cookies to be sent by our GET request.
 * That makes both our sent/received cookies from and to the same file. */
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.log');
 
$output = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);

hope this gives you an idea on how to do it.

1 Like

I believe the easiest way to do it would be using cURL as well (as @sparta said) but instead you might go with another approach :

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.YourRequestWebsite.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
/* enable the header.
 * This will return the resulting header in the $outut variable via curl_exec() below. */
curl_setopt($curl, CURLOPT_HEADER, 1);
 
/* now you execute the operation. */
$output = curl_exec($curl);
 
/* And here you can manipulate your output header info and cookies. */
 
/* if you need to send cookies again after manipulation you can do it easily this way : */
$myCookies = "cookie1=xyz;cookie2=123;"
curl_setopt($curl, CURLOPT_COOKIE, $myCookies);
 
curl_close($curl);

Remember $myCookies can be the resulting cookies from your first request.