6

I have the following php code that logs in to a password protected page and grabs the protected page.The script is working fine but i want to use the login function only once, if i want to grab another protected page within same domain .

i want to use cookie file to open the next protected page instead of using login function again !in another word i just want to bypass the login step for grabbing other protected pages.

Could any one show me how this can be done?

Note: My login function doesnt create any cookies i dont see it in same folder as the script!could any one 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>
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • So what does `cookie.txt` show? – Dave Chen Jun 01 '13 at 17:22
  • Thanks for replies.cookie.txt doesnt get created in the same folder as the php script! i even created it myself but nothing getting written on it! if i manually login to the remote site it will not ask me for password again for many days!i want to achieve same thing while executing this php script multiple times ! because if execute my current php code something i dont get the protected pages since the website refuse me religion my php script so fast! – user1788736 Jun 01 '13 at 17:29
  • Have you seen this? http://stackoverflow.com/questions/895786/how-to-get-the-cookies-from-a-php-curl-into-a-variable – Dave Chen Jun 01 '13 at 17:31
  • Well just store the info that you have already logged in into a variable (if subsequent requests are within the same script) or into the session). – CBroe Jun 01 '13 at 17:31
  • 1
    I'd recommend storing it in the database or on file so you don't need to relogin every time the script is run. – Dave Chen Jun 01 '13 at 17:36
  • dave chen i have two problem here.First i don't know why the cookie.txt doesn't get created. Second if the cookie get created how to use that cookie to get other protected pages without relogin via curl? – user1788736 Jun 01 '13 at 17:40

2 Answers2

2

Use

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

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

You have to have 2 requests. First is login request which fills the cookie file.

You have to check if your cookie file exists is_file($cookieFileLocation), and if it does you can perform second request for download protected content bypassing the login process.

You should note that most of the systems has session expire time, so you have to make login after period of time. I would check html of returned page for login error as mark that i have to login again.

bksi
  • 1,606
  • 1
  • 23
  • 45
  • bksi how should i check for the cookie at the bigining of each call and if the cookie exist bypass login and use that cookie to get protected pages ? problem is that the cookie.txt never get created dont know why! – user1788736 Jun 01 '13 at 17:30
  • 2
    You should use absolute path to create cookie file – bksi Jun 01 '13 at 17:41
  • CURLOPT_COOKIEFILE shows to curl to use this cookie credentials from target cookie file – bksi Jun 01 '13 at 17:43
  • after adding absulte path of directory now cookie is created. Now how to check if cookie exist and if it exist just use that to get the protected page and bypass login? – user1788736 Jun 01 '13 at 18:37
  • As i said, use 2 requests. Check if your file exists. If yes, use request w/o login – bksi Jun 01 '13 at 22:32
2

You need to login first time and then reference cookie file path in subsequent 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;
RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40