123onetwothree
123onetwothree

Reputation: 686

Get page content with session authentication

I looked through Stack Overflow for similar questions but found only pieces of information. So my problem is this:

I want to grab the content of a page let's say : needpage.php (using file_get_contents() + stream_context_create() or using cURL() ) but the page that I need redirects me to a login page ( loginpage.php - <form action=*processlogin.php*> with user and pass).

Do I need to cURL() or file_get_contents() the processlogin.php page first to POST the username and password field, then grab the sessionID and then send another request to the needpage.php I need posting:

$opts = array(
    'http' => array(
        'method' => 'GET',
        'header' => 'Cookie: PHPSESSID=0123456789abcdef0123456789abcdef'
    )
);

What do you think is the right flow? Is it possible that cURL or file_get_contents to store the cookie and then use that cookie for another page?

Upvotes: 0

Views: 402

Answers (1)

Smar
Smar

Reputation: 8611

curl_setopt() lists all kind of useful flags. Maybe CURLOPT_COOKIESESSION would help in your case? The documentation seems to claim so unless I misread it well.

If it doesn’t work, there is CURLOPT_COOKIEJAR, which can be used to save cookie data to a file, after curl_close() has been called.

Then it can be loaded using CURLOPT_COOKIEFILE.

Upvotes: 1

Related Questions