Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of: Form authentication and storing cookie? #85

Open
ryanwinchester opened this issue Nov 15, 2013 · 5 comments
Open

Example of: Form authentication and storing cookie? #85

ryanwinchester opened this issue Nov 15, 2013 · 5 comments

Comments

@ryanwinchester
Copy link

Would really like some more detailed examples.

For example, something like a POST with login credentials from a login form and storing a cookie/session in order to make additional requests

@jago86
Copy link

jago86 commented Feb 10, 2015

I'm agree. Some examples about how handle cookie/session would be really useful.

@simogeo
Copy link

simogeo commented Jun 11, 2015

@fungku @jago86 : do you have any sample code to provide ? I wan to do something similar (see #159)

@anarkopsykotik
Copy link

I agree this would be a good example to have.
I want to use this lib soon to build a scraper which gotta deal with various authentication, so I will prolly need to use session & cookies with form auth. And right now the doc is pretty unclear on the subject.
If I indeed use it and get it working, I will post some example, but I might just go with curl at this point.

@anarkopsykotik
Copy link

Alright, here is a working example. Having something in the doc using multiple different object together would prolly help clarify, as well as explaining how things like sessions, cookies jar etc, work and how/why they should be used together. For example, I'm not quite sure setting the cookie jar myself was really useful there.

        //set form data for authentication
        $data = array('login_email' => $config->login, 'login_password' => $config->password, 'redirect'=>'');
        //create a cookie jar to use for our requests
        $c = new \Requests_Cookie_Jar([]);
        //create a session object for requests. You could set your cookie jar in constructor, but I find it cleaner setting it right after
        //session object basically remember all the headers/options/data for reuse between requests, and merge those with the ones from individual requests params
        $session = new \Requests_Session('https://www.your-site.com/');
        //set headers
        $session->headers['Accept'] = 'text/html';
        $session->useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0';
        $session->options = ['cookies' => $c];//set cookie jar
        //post form authentication, headers/options already set for all requests that use this session
        $response = $session->post('/fr/Login/Authenticate', [], $data );

    //we're now authenticated, now we can retrieve info for the logged in user from another page

        //set form data to retrieve target stats
        $data = array(  'Period-datestart' => $dateD,
                        'Period-dateend' => $dateF);
        $response = $session->request('/statistics/index', [], $data );//default request type is get
         /*print_r($response);
        var_dump($response);*/

        // get html body into a string
        $html = $response->body;

@lmickey
Copy link

lmickey commented Apr 23, 2016

Sorry to drag up an old thread but i think there are some holes in this session. When i dump the session after making the initial request it looks exactly like it did before i make the initial request. I was under the assumption that using a session would automagically populate it with the latest response variables after each request like the cookies, make the referrer the last page requested, any other provided headers that need to be set, and i was hoping it would use the Connection: keep-alive since you're creating it to make multiple requests. I know i can do all this manually i was just under the assumption this was all handled already. Can someone tell me if this is supposed to happen and i'm just doing it wrong (using the exact code in the above comment with different vars) or if this is something that has to be done manually, and the session is just the way you store the variables across. And if i'm doing it manually is there a function built already for this or is that something i will have to also do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants