943,106 Members | Top Members by Rank

Ad:
  • PHP Code Snippet
  • Views: 2827
  • PHP RSS
0

Curl login authentication cookie issues

by on Feb 7th, 2010
OK here is a piece of code I've been working on that actually logs you in and returns what ever Curl sees except i have no idea how to make Curl function correctly with websites that require cookie authentication correctly, can anyone help? =/
PHP Code Snippet (Toggle Plain Text)
  1. <?php
  2.  
  3. //create array of data to be posted
  4. $post_data['username_here'] = "username";
  5. $post_data['password_here'] = "password";
  6.  
  7. //traverse array and prepare data for posting (key1=value1)
  8. foreach ( $post_data as $key => $value) {
  9. $post_items[] = $key . '=' . $value;
  10. }
  11.  
  12. //create the final string to be posted using implode()
  13. $post_string = implode ('&', $post_items);
  14. $post_string = "dest=%2".$post_string ."&submit=next";
  15. //i.e. ?dest=%2F&username=username_here&password=password_here&submit=next
  16.  
  17.  
  18. //create cURL connection
  19. $curl_connection = curl_init("https://my.netspace.net.au/logincheck");
  20.  
  21. //set options
  22. curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); //set time untill cURL times out
  23. curl_setopt($curl_connection, CURLOPT_USERAGENT,
  24. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); // set HTTP User Agent
  25. curl_setopt($curl_connection, CURLOPT_HEADER,1);
  26. curl_setopt($curl_connection, CURLOPT_POST, 1);
  27. curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);//set data to be posted
  28. curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
  29. curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); // follow redirection if any...
  30. curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); //determin if to continue processing code if SSL certificate is verifyed or not...
  31.  
  32. //cookie code ive found that dont seem to work...
  33. /*curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookies.txt");
  34.   curl_setopt($ch, CURLOPT_COOKIE, "cookies.txt");
  35.   curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookies.txt");
  36.   curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true); //return cookies that cURL collects*/
  37.  
  38. //perform our request (execute request)
  39. $result = curl_exec($curl_connection);
  40. curl_close();
  41. echo $result;
  42. ?>
Message:
Previous Thread in PHP Forum Timeline: help deleting a record
Next Thread in PHP Forum Timeline: undefined variable$_get





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC