Many people are asking why the cookie cannot be obtained or stored after the curl login. I will show you how to log on to two phpcurls and read or store the cookie value. Many people are asking why the cookie cannot be obtained or stored after the curl login. Next I will show you how to log on with two php curl and read or store the cookie value.
Script ec (2); script
Prepare two first? Small program to check whether the program has caught the COOKIE value.
Login. php (POST to login. php, login. php will confirm the account/password, and then write the COOKIE value)
| The Code is as follows: |
|
If ($ _ POST ['username'] = 'admin' & $ _ POST ['Password'] = '000000 '){ Setcookie ('U', 'admin '); Header ('location: check_login.php '); } Else { Echo 'failed '; } ?> |
Check_login.php (check whether $ _ COOKIE ['U'] value exists and whether it is 'admin' to determine whether Login is enabled)
| The Code is as follows: |
|
If ($ _ COOKIE ['U'] = 'admin '){ Echo 'login success .'; } Else { Echo 'login failed .'; } ?>
|
Assume that the two files are stored at http: // localhost/login. php/http: // localhost/check_login.php, so write the following program and send the POST to login. php, and then check whether the login is complete through check_login.php. The program is as follows:
Example
| The Code is as follows: |
|
// Get the object? Url? Fixed cookie ,?? Enter cookie.txt (use POST) Function setUrlCookie ($ url, $ postdata) { // $ Cookie_jar = tempnam ('./', 'cooke'); // Create file with unique file name (cookie *) $ Cookie_jar = './cookie.txt '; $ Resource = curl_init (); Curl_setopt ($ resource, CURLOPT_URL, $ url ); Curl_setopt ($ resource, CURLOPT_POST, 1 ); Curl_setopt ($ resource, CURLOPT_POSTFIELDS, $ postdata ); Curl_setopt ($ resource, CURLOPT_COOKIEFILE, $ cookie_jar ); Curl_setopt ($ resource, CURLOPT_COOKIEJAR, $ cookie_jar ); Curl_setopt ($ resource, CURLOPT_RETURNTRANSFER, 1 ); Curl_exec ($ resource ); Return $ resource; } // Which of the following URLs is obtained? Hot? Br/> function getUrlContent ($ resource, $ url) { Curl_setopt ($ resource, CURLOPT_URL, $ url ); Curl_setopt ($ resource, CURLOPT_RETURNTRANSFER, 1 ); $ Content = curl_exec ($ resource ); Return $ content; } // Test: // Post to url and get cookie /* $ Url = 'HTTP: // localhost/login. php '; $ Postdata = 'username = admin & password = 1234 '; $ Resource = setUrlCookie ($ url, $ postdata); // set cookie 'U' => 'admin' or anything
// Get contents (need cookie check) $ Url = 'HTTP: // localhost/check_login.php '; Echo getUrlContent ($ resource, $ url); // Login success. */ ?> |
Function List
■ Resource setUrlCookie ($ url, $ postdata)
■ String getUrlContent ($ resource, $ url)
Usage
■ GetUrlContent (setUrlCookie ($ login_url, $ postdata), $ login_check_url );
SetUrlCookie () is sent to Login by POST. php, and then write the COOKIE into cookie.txt (or use tempnam () to generate a file with a unique name for storage ). use getUrlContent () to pass COOKIE verification to obtain webpage information.
It should be said that the above function should be written as a Class to facilitate the management of curl_init () resources. However, it should be easier to understand to write the function temporarily ~
Let's look at another instance.
| The Code is as follows: |
|
$ Cookie_jar_index = 'cookie.txt '; $ Url = "http://www.111cn.net "; $ Params = "username = dudu & password = ****"; $ Ch = curl_init (); Curl_setopt ($ ch, CURLOPT_URL, $ url ); Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_jar_index ); // Curl_setopt ($ ch, CURLOPT_COOKIE, "fruit = apple; color = red "); // The above Code directly transmits cookie information, not files Curl_setopt ($ ch, CURLOPT_POST, 1 ); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params ); // Curl_setopt ($ ch, CURLOPT_NOBODY, 1); // This cannot be opened; otherwise, the cookie file cannot be generated. Ob_start (); Curl_exec ($ ch ); Curl_close ($ ch ); Ob_clean (); $ Url = "http://www.111cn.net "; $ Ch2 = curl_init (); Curl_setopt ($ ch2, CURLOPT_URL, $ url ); Curl_setopt ($ ch2, CURLOPT_COOKIEFILE, $ cookie_jar_index ); Ob_start (); Curl_exec ($ ch2 ); Curl_close ($ ch2 ); $ Rs = ob_get_contents (); // $ rs is the returned content. Ob_clean (); Print_r ($ rs ); ?> |