I want to file_get_contents and login at http://example.com/index.php. That was the easy part, now since i am logged in i want to file_get_contents at http://example.com/member_area.php, but i don't get the results i was expecting. That is, not loose the session between the 2 file_get_contents. Instead the second file_get_contents result in the http://example.com/index.php which broke my heart.
Here is my code:
<?php
$url_login = 'http://example.com/test/index.php';
$postdata = http_build_query(
array(
'username' => 'user1',
'password' => '123',
'login' => 'true'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url_login, false, $context);
echo $result;
//--So far ok, $result shows me that i am logged in--//
// Now that i am logged in i want to access the member_area.php.
$url_member_area = 'http://example.com/member_area.php';
$result = file_get_contents( $url_member_area, false, $context);
echo $result;
?>
i must achieve that using file_get_contents and not cUrl. I think it has to do with session, but i have no idea where and how to implement.
Thanks in advance, i buy you a Beer!
UPDATED CODE:
<?php
$url_login = 'http://example.com/test/index.php';
$postdata = http_build_query(
array(
'username' => 'user1',
'password' => '123',
'login' => 'true'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url_login, false, $context);
echo $result;
//--So far ok, $result shows me that i am logged in--//
$url_member_area = 'http://example.com/member_area.php';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: username=user1;password=123;login=true"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($url_member_area, false, $context);
// Now that i am logged in i want to access the member_area.php.
echo $file;
Brings me back to index.php asking me to log in, i was hoping to get the member_area.php.
Offer Updated to 2 Beers
*People, i ask for a recipe on how to make Apple pie, you link me to a page on how to wash the apples, and stackoverflow tells me that my question has been answered. Thanks anyway :D