PHP read the method of generating cookie file when the Curl simulates login, _php tutorial

Source: Internet
Author: User
Tags tmp file

PHP reads the method of generating a cookie file at the time of the Curl emulation login,


This example describes how PHP generates cookie files when it reads the Curl emulation login. Share to everyone for your reference. The implementation method is as follows:

A cookie file is saved when using curl in PHP to impersonate a login, such as the following code

Copy CodeThe code is as follows: $login _url = ' XXX ';

$post _fields[' email '] = ' XXXX ';
$post _fields[' password '] = ' XXXX ';
$post _fields[' origurl '] = ' XXX ';
$post _fields[' domain '] = ' xxx.com ';
The cookie file is stored under the Temp folder in the root directory of the Web site
$cookie _file = Tempnam ('./temp ', ' Cookie ');

$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, Curlopt_maxredirs, 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);

Bring a cookie file to access the page you want to visit
$send _url= ' xxx.com ';
$ch = Curl_init ($send _url);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_cookiefile, $cookie _file);
$contents = curl_exec ($ch);
Curl_close ($ch);

Clean up Cookie Files
Unlink ($cookie _file);

Output Web page content
Print_r ($contents);

A temporary file that holds a cookie prefix under the Temp folder, for example: Coo3a98.tmp file
Open this file to get the following code:

To format the file using PHP, you can use the following code to implement

Copy the Code code as follows: <?php
$cookie _folder = dirname (__file__). " /temp ";
$lines = File ($cookie _folder. ' /coo3a98.tmp ');

$trows = ";

foreach ($lines as $line) {
if ($line [0]! = ' # ' && substr_count ($line, "\ t") = = 6) {
$tokens = explode ("\ t", $line);
$tokens = Array_map (' Trim ', $tokens);
$tokens [4] = date (' y-m-d h:i:s ', $tokens [4]);
$trows. = '. Implode (", $tokens). '' . Php_eol;
}
}
Echo '

'. Php_eol. ' '. Php_eol. $trows. ' '. Php_eol. '
';
?>

After the run as shown, it has been written to the table

You're done, if you just read the fields, you can modify them yourself.

I hope this article is helpful to everyone's PHP programming.




http://www.bkjia.com/PHPjc/906109.html www.bkjia.com true http://www.bkjia.com/PHPjc/906109.html techarticle PHP reads the method of generating a cookie file when the curl simulates a login, and this example describes how PHP generates a cookie file when it reads the Curl emulation login. Share to everyone for your reference. Concrete implementation ...

  • 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.