Impersonate a login using curl

Source: Internet
Author: User

    1. <?php
    2. $cookie _path = './'; //Set cookie save path
    3. //-----Log in the form data to be submitted---------------
    4. $vars[' username '] = ' Zhang San ';
    5. $vars[' pwd '] = ' 123 ';
    6. //-------------------------------------
    7. $method _post = true;
    8. //Login Submission URL address (the absolute address of the action in the form)
    9. $url = ' http://****.com/login ';
    10. //----------------------------
    11. $ch = curl_init();
    12. $params[curlopt_url] = $url; //Request URL address
    13. $params[curlopt_header] = true; //Returns the response header information
    14. $params[curlopt_returntransfer] = true; //Whether the result is returned
    15. $params[curlopt_followlocation] = true; // redirect
    16. $params[curlopt_useragent] = ' mozilla/5.0 (Windows NT 5.1; rv:9.0.1) gecko/ 20100101 firefox/9.0.1 ';

    17. $postfields = ";
    18. foreach ($vars as $key => $value){
    19. $postfields . = urlencode($key) . ' = ' . urlencode($value) . ' & ';
    20. }

    21. $params[curlopt_post] = true;
    22. $params[curlopt_postfields] = $postfields;

    23. //To determine if there is a cookie, some words to use directly
    24. if (isset($_cookie[' Cookie_jar ')) && ( $_cookie[' Cookie_jar '] | | is_file($_cookie[' Cookie_jar '] ))
    25. {
    26. $params[curlopt_cookiefile] = $_cookie[' Cookie_jar '];/ / The cookie is judged here
    27. }
    28. Else
    29. {
    30. $cookie _jar = tempnam($cookie _path, ' Cookie '); Generate a cookie file
    31. $params[curlopt_cookiejar] = $cookie _jar; //write cookie information
    32. Setcookie(' Cookie_jar ', $cookie _jar);/ /Save cookie path
    33. }
    34. Curl_setopt_array($ch, $params); //Incoming curl parameter
    35. $content = curl_exec($ch);/ /execution
    36. echo '
    37. ';
    38. echo $content; //Output login result
    39. /*
    40. ---------The login succeeds, request another address again if there are multiple loops to execute---------
    41. Echo '
    42. ‘;
    43. $nexturl = ' http://****.com/test ';
    44. $params [Curlopt_url] = $nexturl;
    45. $params [Curlopt_postfields] = ';
    46. Curl_setopt_array ($ch, $params); Incoming Curl parameter
    47. $content = curl_exec ($ch); Perform
    48. Echo $content; Output request Result
    49. //-------------------------------------------------
    50. */
    51. curl_close($ch); //close connection

    52. ?>

Impersonate a login using curl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.