Jump to content

[SOLVED] Sending 2 Cookies in HTTP header using stream_context_create / file_get_contents


Recommended Posts

I need to send two cookie name/value pairs to the page I am trying to open using file_get_contents, in order for the person viewing the page to have associated dynamic content, but it isn't working, and I'm wondering if I'm setting it up right:

 

if(isset($_COOKIE['bwd_data']) && isset($_COOKIE['bwd'])){
$cookie_val = $_COOKIE['bwd_data'];
$cookie_val2 = $_COOKIE['bwd'];
$opts = array(
  'http'=>array(
	'method'=>"GET",
	'header'=>"Accept-language: en\r\n" .
			  "Cookie: bwd_data=" . $cookie_val . "\r\n" .
			  "Cookie: bwd=" . $cookie_val2
  )
);
$context = stream_context_create($opts);
echo(file_get_contents(url::site('httperrors','http'), FALSE , $context));
}else{
echo(file_get_contents(url::site('httperrors','http')));
}

I didn't even know this was possible with file_get_contents. You may want to take a look at cUrl instead.

 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/page.php?id=205");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIE, "......");
$content = curl_exec($ch);
curl_close($ch);
echo "<pre>".$content."</pre>";

 

It is possible, and documented, ... and I can use the apache_request_headers() function to verify that the headers are reaching the target URL, but for some reason the target URL is not responding to the cookies in the header.

So, I tried it your way, but this doesn't work either:

 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, url::site('httperrors','http'));
$cookie_val = $_COOKIE['bwd_data'];
$cookie_val2 = $_COOKIE['bwd'];
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIE, "bwd=" . $cookie_val2);
curl_setopt($ch, CURLOPT_COOKIE, "bwd_data=" . $cookie_val);
$content = curl_exec($ch);
curl_close($ch);
echo "<pre>".$content."</pre>";

So, apparently all I needed to do was supply a User-Agent in the header, and my original code works:

 

if(isset($_COOKIE['bwd_data']) && isset($_COOKIE['bwd'])){
$cookie_val = $_COOKIE['bwd_data'];
$cookie_val2 = $_COOKIE['bwd'];
$opts = array(
  'http'=>array(
	'method'=>"GET",
	'header'=>"Host: localhost\r\n" .
			  "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10\r\n" .
			  //"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \r\n" .
			  //"Accept-language: en-us,en;q=0.5\r\n" .
			  //"Accept-Encoding: gzip,deflate\r\n" .
			  //"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
			  //"Keep-Alive: 300\r\n" .
			  //"Connection: keep-alive\r\n" .
			  //"Referer: http://localhost/brianswebdesign.com/\r\n" .
			  "Cookie: bwd=" . $cookie_val2 . "; " . "bwd_data=" . $cookie_val . "\r\n"
  )
);
$context = stream_context_create($opts);
echo(file_get_contents(url::site('httperrors','http'), FALSE , $context));
}else{
echo(file_get_contents(url::site('httperrors','http')));
}

I ran some test and it work for me with that :

 

setcookie.php

<?php

setcookie("cookie1", "itwork");
setcookie("cookie2", "yeah");

?>

 

displaycookie.php

<?php

print_r($_COOKIE);

?>

 

test.php

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/displaycookie.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIE, "cookie1=".$_COOKIE['cookie1']."; "."cookie2=".$_COOKIE['cookie2']);
$content = curl_exec($ch);
curl_close($ch);
echo "<pre>".$content."</pre>";

?>

Actually, I did make changes there too, but as soon as the User-Agent is commented out, it doesn't work anymore. You are right about that line being something critical, because initially I had it wrong, and it was only through examining some request headers that I found the proper way to set more than one cookie.

 

Now that I'm testing on the production server, it doesn't work again! I have to go to bed, and try again in the morning. Too tired to go on...

The target site may forbid request without a useragent (or request with whatever the file_get_contents useragent is)

 

You can set it in curl too :

curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');

In theory, both ways are sending the cookies, and both ways can be determined to work based on the fact that the cookie IS in the header, but I still can't get either way to work on the production server.

 

I thought for sure when I woke up that it would be because of the line endings difference between my WAMP server at home and normal Linux shared hosting on the production server, but after changing the line endings from \r\n to \n, it still didn't work.

After days searching for an answer, I found out that when PHP is compiled --with-curlwrappers the header must be in the form of an array, and not just a string. This isn't documented, but I found this answer by browsing the bug reports for the stream_context_create() function.

 

I am relieved that I finally found the answer I was looking for, but I spent countless hours searching for the answer, and even though it is in the bug reports, apparently nobody ever took the time to add it to the documentation. I wish I could add it, but it is not allowed according to "the rules".

  • 1 year later...

After days searching for an answer, I found out that when PHP is compiled --with-curlwrappers the header must be in the form of an array, and not just a string. This isn't documented, but I found this answer by browsing the bug reports for the stream_context_create() function.

 

I am relieved that I finally found the answer I was looking for, but I spent countless hours searching for the answer, and even though it is in the bug reports, apparently nobody ever took the time to add it to the documentation. I wish I could add it, but it is not allowed according to "the rules".

 

Your post saved me... Can you tell me exactly what you mean by "the header must be in the form of an array".

 

$requestHeaders = array('http' => array(

  'method'=>"GET",

  'header'=> "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10\r\n" .

  "CustomHeader: ".CUST_HEADER_VALUE."\r\n".

"CustomHeader1: ".CUST_HEADER_VALUE1."\r\n",

)

);

 

The above is the normal way, and you say array, can you please show us, since I am sure as you are aware lots of people are having this issue and no where to go.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.