Php reads CURL to generate a Cookie file during simulated Login

Source: Internet
Author: User
This article mainly introduces how PHP reads CURL to generate Cookie files during simulated login, including the use of curl and cookie operations, which are of great practical value, for more information about how to generate a Cookie file when PHP reads a CURL simulated login, including the use of curl and cookie operations, this article is of great practical value, for more information, see

This example describes how PHP reads CURL to generate a Cookie file during simulated login. Share it with you for your reference. The specific implementation method is as follows:

When using the CURL in PHP to simulate logon, a Cookie file will be saved, for example, the following code

The Code is as follows:

$ Login_url = 'xxx'; $ post_fields ['email '] = 'xxx'; $ post_fields ['Password'] = 'xxx '; $ post_fields ['url url'] = 'xxx'; $ post_fields ['domain '] = 'xxx. com '; // The cookie file is stored in the temp folder of the website root directory $ cookie_file = tempnam ('. /temp ', 'cookies'); $ ch = curl_init ($ login_url); curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.9.1.5) Gecko/20091102 Firefox/3.5.5 '); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, batch, 1); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1); curl_setopt ($ ch, CURLOPT_POST, 1 ); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_fields); curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); curl_exec ($ ch); curl_close ($ ch); // carries the cookie file, access the page to be accessed $ send_url = 'xxx. com '; $ ch = curl_init ($ send_url); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, batch, 1); curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file); $ contents = curl_exec ($ ch); curl_close ($ ch); // clear the cookie file unlink ($ cookie_file ); // output the webpage content print_r ($ contents );

Save a temporary file with the cookie prefix in the temp folder, for example, the coo3A98. tmp file.
Open this file and get the following code:

To format the file using php, use the following code:

The Code is as follows:

 ' . implode('', $tokens) . '' . PHP_EOL;      }  }  echo '
 
  '.PHP_EOL.'
  
   '.PHP_EOL.$trows.'
  '.PHP_EOL.'
 
'; ?>

As shown in figure, it has been written to the table.

As a result, you can modify the field if you only want to read it.

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.