Php post simulation code Summary (the POST method is not absolutely secure)

Source: Internet
Author: User
GET behavior is relatively simple, and POST is more complicated

GET behavior is relatively simple, and POST is more complicated

There are two ways to choose from: first, handwriting code. Second: Use the HttpClient php class library
Method 1:
The Code is as follows:
<? PHP
$ Flag = 0;
// Data to be post
$ Argv = array (
'Var1' => 'abc ',
'Var2' => '');
// Construct the string to post
Foreach ($ argv as $ key => $ value ){
If ($ flag! = 0 ){
$ Params. = "&";
$ Flag = 1;
}
$ Params. = $ key. "="; $ params. = urlencode ($ value );
$ Flag = 1;
}
$ Length = strlen ($ params );
// Create a socket connection
$ Fp = fsockopen ("127.0.0.1", 80, $ errno, $ errstr, 10) or exit ($ errstr. "--->". $ errno );
// Construct the post Request Header
$ Header = "POST/mobile/try. php HTTP/1.1 ";
$ Header. = "Host: 127.0.0.1 ";
$ Header. = "Referer:/mobile/sendpost. php ";
$ Header. = "Content-Type: application/x-www-form-urlencoded ";
$ Header. = "Content-Length:". $ length ."";
$ Header. = "Connection: Close ";
// Add the post string
$ Header. = $ params ."";
// Send post data
Fputs ($ fp, $ header );
$ Inheader = 1;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 1024); // only the returned data on the page is displayed when the request packet header is removed.
If ($ inheader & ($ line = "n" | $ line = "")){
$ Inheader = 0;
}
If ($ inheader = 0 ){
Echo $ line;
}
}
Fclose ($ fp );
?>

The second method is to use the httpclient class.
The Code is as follows:
$ PageContents = HttpClient: quickPost ('HTTP: // example.com/someForm', array (
'Name' => 'some name ',
'Email '=> 'email @ example.com'
));

Use the httpclient class library, you can go to the official download of the latest class library, the official address: http://scripts.incutio.com/httpclient/index.php
Additional php httpclient usage
Static method to obtain the webpage:
The Code is as follows:
$ PageContents = HttpClient: quickGet ('HTTP: // bankcha.com ')

Get Method
The Code is as follows:
$ Client = new HttpClient ('bankcha. com ');
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Get the Get method with debugging
PHP code
$ Client = new HttpClient ('bankcha. com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Get method with automatic steering
PHP code
$ Client = new HttpClient ('www .bankcha.com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Check whether the page exists
PHP code
$ Client = new HttpClient ('bankcha. com ');
$ Client-> setDebug (true );
If (! $ Client-> get ('/thispagedoesnotexist ')){
Die ('an error occurred: '. $ client-> getError ());
}
If ($ client-> getStatus () = '000000 '){
Echo 'page does not exist! ';
}
$ PageContents = $ client-> getContent ();
Counterfeit Client
PHP code
$ Client = new HttpClient ('bankcha. com ');
$ Client-> setDebug (true );
$ Client-> setUserAgent ('mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.3a) Gecko/8080 ');
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Log on to verify and request a webpage
PHP code
$ Client = new HttpClient ('bankcha. com ');
$ Client-> post ('/login. php', array (
'Username' => 'simon ',
'Password' => 'ducks'
));
If (! $ Client-> get ('/private. php ')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
HTTP Authorization
PHP code
$ Client = new HttpClient ('bankcha. com ');
$ Client-> setAuthorization ('username', 'Password ');
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
$ PageContents = $ client-> getContent ();
Output header information
PHP code
$ Client = new HttpClient ('bankcha. com ');
If (! $ Client-> get ('/')){
Die ('an error occurred: '. $ client-> getError ());
}
Print_r ($ client-> getHeaders ());
Sets the maximum number of domain redirection times.
PHP code
$ Client = new HttpClient ('www .bankcha.com ');
$ Client-> setDebug (true );
$ Client-> setMaxRedirects (3 );
$ Client-> get ('/');

Php fsockopen forges post and get Methods
Fsockopen spoofs the post and get methods. If you are looking for php processing code that spoofs the post and get methods, this is good.
The Code is as follows:
// Fsocket simulate post submission
$ Purl = "http: // localhost/netphp/test2.php? Uu = rrrrrrrrrrrr ";
Print_r (parse_url ($ url ));
Sock_post ($ purl, "uu = 55555555555555555 ");
// Fsocket simulate get submission
Function sock_get ($ url, $ query)
{
$ Info = parse_url ($ url );
$ Fp = fsockopen ($ info ["host"], 80, $ errno, $ errstr, 3 );
$ Head = "GET". $ info ['path']. "? ". $ Info [" query "]." HTTP/1.0rn ";
$ Head. = "Host:". $ info ['host']. "rn ";
$ Head. = "rn ";
$ Write = fputs ($ fp, $ head );
While (! Feof ($ fp ))
{
$ Line = fread ($ fp, 4096 );
Echo $ line;
}
}
Sock_post ($ purl, "uu = rrrrrrrrrrrrrr ");
Function sock_post ($ url, $ query)
{
$ Info = parse_url ($ url );
$ Fp = fsockopen ($ info ["host"], 80, $ errno, $ errstr, 3 );
$ Head = "POST". $ info ['path']. "? ". $ Info [" query "]." HTTP/1.0rn ";
$ Head. = "Host:". $ info ['host']. "rn ";
$ Head. = "Referer: http: //". $ info ['host']. $ info ['path']. "rn ";
$ Head. = "Content-type: application/x-www-form-urlencodedrn ";
$ Head. = "Content-Length:". strlen (trim ($ query). "rn ";
$ Head. = "rn ";
$ Head. = trim ($ query );
$ Write = fputs ($ fp, $ head );
While (! Feof ($ fp ))
{
$ Line = fread ($ fp, 4096 );
Echo $ line;
}
}
?>

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.