sKunKbad Posted May 14, 2009 Share Posted May 14, 2009 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'))); } Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/ Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833891 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833897 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833904 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 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'))); } Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833907 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833909 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 Are you sure it's the useragent ? Cookie are on separate line on the first post, on the same line in the last. Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833911 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833920 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 14, 2009 Share Posted May 14, 2009 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'); Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-833921 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-834106 Share on other sites More sharing options...
sKunKbad Posted May 14, 2009 Author Share Posted May 14, 2009 Anyone? I'm just looking for a reason why this works at home on WAMP, but not on the production server (Linux)??? Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-834366 Share on other sites More sharing options...
sKunKbad Posted May 15, 2009 Author Share Posted May 15, 2009 If anybody can take a look at my phpinfo(), I've made it available here: http://www.ramseys-auto-detailing.com/phpinfo.gif Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-834998 Share on other sites More sharing options...
sKunKbad Posted May 16, 2009 Author Share Posted May 16, 2009 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". Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-835138 Share on other sites More sharing options...
djokodonev Posted August 12, 2010 Share Posted August 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/158084-solved-sending-2-cookies-in-http-header-using-stream_context_create-file_get_contents/#findComment-1098359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.