Simulate login with PHP

Source: Internet
Author: User
Some people often ask questions about simulated login. In fact, the principle is very simple. you just need to save the SessionID. Today, it took an hour to write a function for your reference.

Some people often ask questions about simulated login. In fact, the principle is very simple. you just need to save the SessionID. Today, it took an hour to write a function for your reference, the header information returned by the website. The source code is as follows:

  1. /*
  2. * Get the webpage content
  3. * Parameter: $ host [in] string
  4. * Host name (for example, www.shanghaicom)
  5. * Parameter: $ method [in] string
  6. * Submission method: POST, GET, HEAD... and add relevant parameters (for detailed syntax, see RFC1945, RFC2068)
  7. * Parameter: $ str [in] string
  8. * Submitted content
  9. * Parameter: $ sessid [in] string
  10. * PHP SESSIONID
  11. *
  12. * @ Return webpage content string
  13. */
  14. Function GetWebContent ($ host, $ method, $ str, $ sessid = '')
  15. {
  16. $ Ip = gethostbyname ($ host );
  17. $ Fp = fsockopen ($ ip, 80 );
  18. If (! $ Fp) return;
  19. Fputs ($ fp, "$ methodrn ");
  20. Fputs ($ fp, "Host: $ hostrn ");
  21. If (! Emptyempty ($ sessid ))
  22. {
  23. Fputs ($ fp, "Cookie: PHPSESSID = $ sessid; path =/; rn ");
  24. }
  25. If (substr (trim ($ method), 0, 4) = "POST ")
  26. {
  27. Fputs ($ fp, "Content-Length:". strlen ($ str). "rn"); // do not forget to specify the Length.
  28. }
  29. Fputs ($ fp, "Content-Type: application/x-www-form-urlencodedrnrn ");
  30. If (substr (trim ($ method), 0, 4) = "POST ")
  31. {
  32. Fputs ($ fp, $ str. "rn ");
  33. }
  34. While (! Feof ($ fp ))
  35. {
  36. $ Response. = fgets ($ fp, 1024 );
  37. }
  38. $ Hlen = strpos ($ response, ""); // "" in LINUX ""
  39. $ Header = substr ($ response, 0, $ hlen );
  40. $ Entity = substr ($ response, $ hlen 4 );
  41. If (preg_match ('/PHPSESSID = ([0-9a-z]);/I', $ header, $ matches ))
  42. {
  43. $ A ['sessid '] = $ matches [1];
  44. }
  45. If (preg_match ('/Location: ([0-9a-z _? = & #.])/I ', $ header, $ matches ))
  46. {
  47. $ A ['location'] = $ matches [1];
  48. }
  49. $ A ['content'] = $ entity;
  50. Fclose ($ fp );
  51. Return $;
  52. }
  53.  
  54. /* Construct the username and password string */
  55. $ Str = ("username = test & password = test ");
  56. $ Response = GetWebContent ("localhost", "POST/login. php HTTP/1.0", $ str );
  57. Echo $ response ['location']. $ response ['content']."
    ";
  58. Echo $ response ['sessid ']."
    ";
  59. If (preg_match ('/error. php/I', $ response ['location'])
  60. {
  61. Echo "login failed
    ";
  62. } Else {
  63. Echo "login successful
    ";
  64. // User. php cannot be accessed because the sessid parameter is not included.
  65. $ Response = GetWebContent ("localhost", "GET/user. php HTTP/1.0 ",'','');
  66. Echo $ response ['location']."
    "; // Result: error. php? Errcode = 2
  67.  
  68. // You can access user. php
  69. $ Response = GetWebContent ("localhost", "GET/user. php HTTP/1.0", '', $ response ['sessid']);
  70. Echo $ response ['location']."
    "; // Result: user. php
  71. }
  72. ?>

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.