How to reuse php curl cookie and bypass login steps in subsequent quotes of the script?

I have the following PHP code that enters a password protected page and captures a protected page. The script works fine, but I want to use the login function only once if I want to capture another secure page inside the same domain.

I want to use a cookie to open the next secure page, instead of using the login feature again! In another word, I just want to bypass the login step to capture other secure pages.

Can someone show me how to do this?

Note. My login function does not create any cookies that I do not see in the same folder as the script. Can someone tell me why?

<? $ch=login(); $html=downloadUrl('http://www.example.com/page1.asp', $ch); ////echo $html; function downloadUrl($Url, $ch){ curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/"); curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = curl_exec($ch); return $output; } function login() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.asp'); //login URL curl_setopt ($ch, CURLOPT_POST, 1); $post_array = array( 'txtUserName'=>'brad', 'txtPassword'=>'bradpassword', ); curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_array); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec ($ch); return $ch; } ?> <html> <br> <textarea rows="30" cols="150"><?PHP print_r($html); ?></textarea> </html> 
+6
source share
2 answers

Use

 curl_setopt($ch,CURLOPT_COOKIEJAR, $cookieFileLocation); curl_setopt($ch,CURLOPT_COOKIEFILE, $cookieFileLocation); 

In the second request, where $ cookieFileLocation is the location of your cookie.

You must have 2 requests. The first is the login that populates the cookie.

You need to check if the is_file($cookieFileLocation) cookie is_file($cookieFileLocation) , and if possible, you can execute a second request for the secure content to be downloaded, bypassing the login process.

You should notice that most systems have a session end time, so you must login after a period of time. I would check the html of the returned page for a login error as a sign that I need to login again.

+2
source

You need to log in first, and then specify the path to the cookie in the next request.

 function curlPost($url,$postData){ $ch= curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData, CURLOPT_FOLLOWLOCATION => true, CURLOPT_CONNECTTIMEOUT=>30, CURLOPT_SSL_VERIFYPEER=>false, CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", CURLOPT_COOKIESESSION => true, CURLOPT_COOKIEFILE => 'cookie.txt', CURLOPT_COOKIEJAR => 'cookie.txt' )); $output = curl_exec($ch); curl_close( $ch ); return $output; } $postData = array( 'email' => ' aryan022@gmail.com ', 'password' => 'aryan022', 'redirect_to' => 'http://localhost/cakephp/account ' ); $output=curlPost("http://localhost/cakephp/login",$postData); /*use for subsequest request without passing all postData once login $postData = array(); $output=curlPost("http://localhost/cakephp/account",$postData); */ echo $output; 
+2
source

Source: https://habr.com/ru/post/946321/


All Articles