0

How do I use my current browser session and create a GET request in PHP based on that?

I tried the following:

$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://localhost/x/");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

$headers=array();

$data=explode("\n",$output);

$headers['status']=$data[0];

array_shift($data);

foreach($data as $part){
    $middle=explode(":",$part);
    $headers[trim($middle[0])] = trim($middle[1]);
}

//print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";

after checking, it seems that it is not really using my session to create the GET request, rather, it is creating an entire new GET request.

I also tried to pull the headers only of a get_headers() but that seemed to have the same issue for some reason. Is there a way to pull up my current session from a browser and use that in a GET request rather than having a new one every time? (I will be in the same machine so the session is mine). In short, I want to initiate a GET request using my current session.

rullzing
  • 622
  • 2
  • 10
  • 22
  • Why you should do curl if you can access your data directly (assuming you are logged in). In your case you are doing recursive http request which is not good and it's a bad implementation. – Mark Nov 09 '17 at 02:34
  • @Mark How can I access data directly? I don't want to be doing this manually. – rullzing Nov 09 '17 at 02:35
  • in your "http://localhost/x/" if you have an output that is separated in linebreaks you can use those values directly by bootstrapping your php files. using curl to access dynamic data in same host is a real bad practice. – Mark Nov 09 '17 at 02:45
  • @Mark I'm not necessarily requesting things from localhost only. So if I want to go to google.com for instance from localhost, I want my google's session to be there. What's your suggestion? – rullzing Nov 09 '17 at 02:48
  • If you really want to do those things like "Scraping" a webpage you have to do curl authentication. Saving your cookies by setting php curl options (CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE) would be handy also. https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl – Mark Nov 09 '17 at 02:48
  • @Mark Those seem to require the username and password, which is not what I want to do. – rullzing Nov 09 '17 at 02:56

0 Answers0