Hey,
i would like to make 2 or more file_get_contents in 1 session. Please show me how is done. Here is what is have so far:

<?php
$url_login = 'http://test.com/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--//
Here i am tring to make the secont file_get_contents but the session is lost and i get the login page.


$url_member_area = 'http://test.com/member_area.php';

$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;
?>

    If you mean you want a single session on test.com, you have to remember that HTTP is a stateless protocol. There'll be a session identifier sent back from the site when you log in, probably a cookie, and you have to include that with any subsequent requests to the site so the server can match them up with the session.

    In that case basically you will need to use the cURL interface since this is the sort of thing that library is for. (As an alternative you could try using the $http_response_header variable, or perhaps, if you don't need the $result of the login request, the get_headers() function.)

      Thanks a lot for taking the time to reply man. Unfortunately curl is not an option.
      Pls, could I have an example on how to store those cookies and how to set then later for multiple request after the log in.?
      In previews script I did use curl with cookie jar and cookie file. Was pretty simple, here I have no idea.

        Write a Reply...