Jump to content

stream_context_create() and disappearing cookies


sKunKbad

Recommended Posts

I've been trying to pass a cookie along to one of my sub domains, and the I'm getting an error:

 

An error #2 occurred in script 'C:\xampp\htdocs\optifit-hcg\application\controllers\test.php' on line 66:

file_get_contents(http://localhost.optifit-hcg/horz_text_menu) [function.file-get-contents]: failed to open stream: HTTP request failed!

 

The idea is that the script on the sub domain would return some data based on the cookie contents, but when I print_r() both $_SESSION and $_COOKIE the arrays are blank.

 

Please let me know if you see anything wrong:

 

$opts['http']['method'] = 'GET';

$opts['http']['user_agent'] = $_SERVER['HTTP_USER_AGENT'];

$cookie_string = 'Cookie: ';

// If there are cookies, send them with the request
if( count( $_COOKIE ) > 0 )
{
$i = 1;

foreach( $_COOKIE as $k => $v )
{
	if( $i !== 1 )
	{
		$cookie_string .= '; ';
	}

	$cookie_string .= $k . '=' . urlencode( $v );

	$i++;
}

// Development environment not compiled with curl wrappers
if( ENVIRONMENT != 'development' )
{
	$opts['http']['header'][] = $cookie_string;
}
else
{
	$opts['http']['header'] = $cookie_string;
}
}

$context = stream_context_create( $opts );

$contents = file_get_contents( 'http://localhost.optifit-hcg/horz_text_menu', FALSE, $context );

echo $contents;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.