dahype Posted August 14, 2009 Share Posted August 14, 2009 Hello buddies, i am working on a php that must 1.login to a website with user/password 2.fill in a second form inside this website 3.submit & get the result My script actually can login but i get "rejected" after some second with the message that my session is over. This is probably a cookie problem but i can't figure it out. The script is the following: <?php $cookie_jar = tempnam('/','cookie'); $url = "http://example.com/index.php"; $POSTFIELDS = 'field1=value1&username=myuser&password=mypass'; $reffer = "https://example.com/"; $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; $cookie_file_path = "/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_REFERER, $reffer); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); $url = "https://example.com/second.php?fromlogin=true"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,null); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_REFERER, null); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result2 = curl_exec($ch); $url = "https://example.com/third.php"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,null); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_REFERER, "https://example.com/second.php?fromlogin=true"); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result3 = curl_exec($ch); $file = tempnam('/','output'); $handle = fopen("output", "w+"); fwrite ( $handle , "one:".$result."\ntwo:".$result2."\nthree:".$result3 ); fclose($handle); curl_close($ch); ?> and what i have in output file is one:<html><head><meta http-equiv="Refresh" content="5;url=https://example.com/index.php?part=timeout"></head><body></body></html> two:<html><head><meta http-equiv="Refresh" content="0;url=index.php?part=timeout"></head><body></body></html> three:function Set_Cookie(name, value, expires, path, domain, secure ) { // set time, it's in milliseconds var today = new Date(); today.setTime( today.getTime() ); /* if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24 */ var expires_date = new Date( today.getTime() + (expires) ); document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); } Set_Cookie("windowname", '', 2147483647, '/', '', ''); does anybody know where i am doing it wrong? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 14, 2009 Share Posted August 14, 2009 Hi dahype, It may be that you first have to the second website's root (www.example.com) and set the cookie there before you attempt anything else. Might be worth a go. Quote Link to comment Share on other sites More sharing options...
dahype Posted August 14, 2009 Author Share Posted August 14, 2009 and how do i set the cookie for the www.example.com ? Hi dahype, It may be that you first have to the second website's root (www.example.com) and set the cookie there before you attempt anything else. Might be worth a go. Quote Link to comment Share on other sites More sharing options...
aschk Posted August 14, 2009 Share Posted August 14, 2009 After each request you need to update your cookie info, and maintain it, failure to do so could cause a "logout" if the session identifier changes. Are you sure your cookie jar/file is being written correctly? Quote Link to comment Share on other sites More sharing options...
dahype Posted August 14, 2009 Author Share Posted August 14, 2009 sorry for the question, i am quite new to it: how do i update my cookie info every time? After each request you need to update your cookie info, and maintain it, failure to do so could cause a "logout" if the session identifier changes. Are you sure your cookie jar/file is being written correctly? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 14, 2009 Share Posted August 14, 2009 You're script isn't calling the cookie file, just the cookie file path location twice, so change every occurence of: curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); to: curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); Also might be worth changing: $cookie_jar = tempnam('/','cookie'); to: $cookie_jar = ('cookie.txt'); Also, if you don't need cookie path info, again just edit every occurence of: curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); to: curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); and delete the $cookie_file_path = "/"; string completely. Then, you need to make 3 separate cURL requests for each operation. One to login to the site, one to fill in the form and one to submit the form. Quote Link to comment Share on other sites More sharing options...
dahype Posted August 15, 2009 Author Share Posted August 15, 2009 Then, you need to make 3 separate cURL requests for each operation. One to login to the site, one to fill in the form and one to submit the form. Ok, so for the index.php i need to make 3 request, the first with $url = "example.com/index.php" the second ? the third with $url = "example.com/index.php" and $POSTFIELDS correctly set for the login process. Did i get it right? I don't understand what the second request will contain. thx Quote Link to comment Share on other sites More sharing options...
dahype Posted August 16, 2009 Author Share Posted August 16, 2009 I still can figure it out, can somebody help please? thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.