Example of saving login cookie information using php simulated logon

Source: Internet
Author: User
Example of saving login cookie information using php simulated logon

  1. Function write_cookie ($ cookie, $ db)

  2. {
  3. $ Fp = fopen ('/tmp/cookie.txt', 'w ');
  4. Fwrite ($ fp, $ cookie );
  5. Fclose ($ fp );
  6. }

  7. Function get_cookie ($ db)

  8. {

  9. $ Handle = @ fopen ("/tmp/cookie.txt", "r ");

  10. If ($ handle ){
  11. While ($ buffer = fgets ($ handle, 4096 ))! = False ){
  12. $ Cookie = trim ($ buffer );
  13. }
  14. Fclose ($ handle );
  15. }

  16. Return $ cookie;

  17. }

  18. Function openhttp ($ url, $ post = ", $ cookie =", $ agent = ", $ referfer =", $ origin = "", $ host = "", $ content_type = "", $ is_header = 1 ){

  19. $ Ch = curl_init ();
  20. Curl_setopt ($ ch, CURLOPT_URL, $ url );

  21. $ Header = array ();

  22. If (! Empty ($ host )){
  23. $ Header [] = "Host: zzip.ip518.com: 900 ″;
  24. }

  25. If (! Empty ($ origin )){

  26. $ Header [] = "Origin: http://zzip.ip518.com: 900 ″;
  27. }
  28. If (! Empty ($ content_type )){
  29. $ Header [] = "Content-Type: application/x-www-form-urlencoded ";
  30. }

  31. If (count ($ header )){

  32. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
  33. }

  34. If (! Empty ($ cookie) curl_setopt ($ ch, CURLOPT_COOKIE, $ cookie );

  35. // Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
  36. If (! Empty ($ referfer) curl_setopt ($ ch, CURLOPT_REFERER, $ referfer );
  37. Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31 ″);
  38. If (! Empty ($ post) curl_setopt ($ ch, CURLOPT_POST, 1 );
  39. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  40. If (! Empty ($ post) curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post );
  41. If ($ is_header) curl_setopt ($ ch, CURLOPT_HEADER, 1 );

  42. $ Return = curl_exec ($ ch );

  43. Curl_close ($ ch );
  44. Return $ return;
  45. }

  46. Function login ($ username, $ password, $ login_url = "http://zzip.ip518.com: 900/login. asp ")

  47. {
  48. $ Content = ";
  49. $ Cookies = ";
  50. $ Content = openhttp ("http://zzip.ip518.com: 900/login. asp", "name = $ username & pass = $ password & enter = ");
  51. $ Cookies = get_items ($ content, array ("Set-Cookie:"), array (";");

  52. $ Cookie = implode (";", $ cookies );

  53. Write_cookie ($ cookie );

  54. Return $ cookie;

  55. }

  56. Login ("account", "password ");

Note: after successful logon, the cookie information is written to the/tmp/cookie.txt file. The next time you directly read this file, you can perform operations on this cookie file to log on.

>>> More php simulated logon articles. topic links: php simulated logon php curl simulated logon tutorials

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.