In php, fsockopen imitates post and get. _ PHP Tutorial

Source: Internet
Author: User
In php, fsockopen imitates post and get. In php, the fsockopen function can simulate the user's access to some websites and also carry some common information. if the browser, IP address, post, get, and other data, next, I will introduce the fsockopen function in php to simulate the user's access to some websites and to bring some common information, if the browser, IP address, post, get, and other data, next I will introduce you to you separately.

If you want to use the fsockopen function, you must set allow_url_fopen = On to enable in php. ini.

Example

Fsockopen () Example

The code is as follows:

$ Fp = fsockopen ("www.example.com", 80, $ errno, $ errstr, 30 );
If (! $ Fp ){
Echo "$ errstr ($ errno)
N ";
} Else {
$ Out = "GET/HTTP/1.1rn ";
$ Out. = "Host: www. example. comrn ";
$ Out. = "Connection: Closernrn ";
Fwrite ($ fp, $ out );
While (! Feof ($ fp )){
Echo fgets ($ fp, 128 );
}
Fclose ($ fp );
}
?>


Counterfeit post

POST an HTTP request (URL) and obtain the returned value

The code is as follows:

$ Srv_ip = '192. 168.1.5 '; // your target service address.

$ Srv_port = 80; // port

$ Url = 'http: // localhost/fsock. php'; // the specific address for receiving your post URL

$ Fp = '';

$ Errno = 0; // handle errors

$ Errstr = ''; // handle errors

$ Timeout = 10; // The duration of a connection is interrupted.

$ Post_str = "username = demo & password = hahaha"; // content to be submitted.

// Open the Socket link of the network.

$ Fp = fsockopen ($ srv_ip, $ srv_port, $ errno, $ errstr, $ timeout );

If (! $ Fp ){

Echo ('fp fail ');

}

$ Content_length = strlen ($ post_str );

$ Post_header = "POST $ url HTTP/1.1rn ";

$ Post_header. = "Content-Type: application/x-www-form-urlencodedrn ";

$ Post_header. = "User-Agent: MSIErn ";

$ Post_header. = "Host:". $ srv_ip. "rn ";

$ Post_header. = "Content-Length:". $ content_length. "rn ";

$ Post_header. = "Connection: closernrn ";

$ Post_header. = $ post_str. "rnrn ";

Fwrite ($ fp, $ post_header );

$ Inheader = 1;

While (! Feof ($ fp) {// test whether the file pointer is at the end of the file

$ Line = fgets ($ fp, 1024 );

// Remove the header information of the request packet

If ($ inheader & ($ line = "n" | $ line = "rn ")){

$ Inheader = 0;

}

If ($ inheader = 0 ){

Echo $ line;

}

}

Fclose ($ fp );

Unset ($ line );

?>

Brief description: The second line of the code is your IP address or domain name, and the fourth line is the specific address of the page you want to POST. In this example, the content of fsock. php is as follows:

The code is as follows:

Echo "username:". $ _ POST ['username']."
";

Echo "password:". $ _ POST ['password'];

?>


Result:

Username: demo

Password: hahaha


Forge get


At the same time, the post and get methods are forged.

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;
}
}
?>


Token and other data. next I will introduce them to you...

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.