PHP httpclient Get cookies

Source: Internet
Author: User
Tags setcookie
Today, the first day to learn to use PHP, is to implement a remote login in PHP, a ASPX page, that is, PHP login password passed through the user name, directly to the ASPX interface login. The ASPX interface is also written by myself, so logic is well controlled. I'm a Java programmer, C # and PHP are all halfway decent for me, dealing with writing.


Because Java is by httpclient ready-made jar package, so Baidu PHP httpclient, sure enough to find one.

The official address of the download is: http://scripts.incutio.com/httpclient/index.php

Download the compressed package, extract a file HttpClient.class.php after decompression. Introduced into the project, write a test page to invoke. The official example is a lot of get, I want to use a post to obtain the cookie, in fact, the service is written by themselves, get is also the line, but originally planned to use post, on the post bar.


Read the HttpClient.class.php this file, although PHP syntax is basically Greek, but guess to come.


<span style= "FONT-SIZE:14PX;" >require_once (' HttpClient.class.php ');
$params = Array (' name ' => ' xxx ', ' password ' => ' 123 ');
$client = new HttpClient (' 172.18.8.23 ', ' 8888 '); 
$client->setdebug (TRUE);//Open debug mode
$path = "/autologin.aspx";
if ($client->post ($path, $params)) {  
  Print_r ($client->getheaders ());
  echo "<----------1----------->";
  Print_r ($client->getcookies ());
   echo "<--------2------------>";
  Print_r ($client->getstatus ());
   echo "<---------3----------->";
  Print_r ($client->getcontent ());
  echo "<---------4----------->";
}
Else
	echo ' false ';</span>


After the page is executed, the rest can be returned, and the name of the header that is printed is seen in the cookie set, but the array of cookies is always empty and why.

Then look at the HttpClient.class.php code, found that there are the following statements



This cookie_host, the entire file is not set up an empty string, so the array is not worth it. Delete this condition, in execution, you can successfully get the cookie.

And then through the Setcookie to get the cookie set into the access page is already automatic login. Don't forget to set domain and path when setcookie.



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.