-
Notifications
You must be signed in to change notification settings - Fork 493
-
Notifications
You must be signed in to change notification settings - Fork 493
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
Comments
I'm agree. Some examples about how handle cookie/session would be really useful. |
I agree this would be a good example to have. |
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; |
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? |
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
The text was updated successfully, but these errors were encountered: