0

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

HeyHo
  • 1
  • 1
  • 2
    If encountering unexpected behavior 'breaks your heart' when you're just starting out as a programmer, you're in for a wild ride. Take it easy. See [this answer](https://stackoverflow.com/questions/3431160/send-cookie-with-file-get-contents). – Ro Achterberg Nov 10 '20 at 17:12
  • Does this answer your question? [Send cookie with file\_get\_contents](https://stackoverflow.com/questions/3431160/send-cookie-with-file-get-contents) – Ro Achterberg Nov 10 '20 at 17:12
  • @RoAchterberg I'm trying to figure out how to implement the "Cookie: user=3345; pass=abcd" to my code :D – HeyHo Nov 10 '20 at 17:20
  • That's great. Tell us clearly if you need help. – Ro Achterberg Nov 10 '20 at 17:45
  • @RoAchterberg Is it correct the way i did it? Am i missing something? – HeyHo Nov 10 '20 at 17:54

0 Answers0