Php curl logon cookie value access example

Source: Internet
Author: User
Tags curl set cookie

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: Copy code
<? Php
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: Copy code


<? Php
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: Copy code

<? Php

// 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: Copy code

<? Php
$ 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 );
 
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.