PHP Fsockopen imitate Post and get detailed _php tutorial

Source: Internet
Author: User
Tags fread
In PHP fsockopen function can imitate the user to visit some Web site and can also bring some common information, if the browser, Ip,post,get and so on data, I will introduce each of you.

If you want to use the Fsockopen function we must set the Allow_url_fopen = on to ON in PHP.ini.

Cases

Fsockopen () Example

The code is as follows Copy Code

$fp = Fsockopen ("www.example.com", $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);
}
?>


Forge Post

POST http request (URL) and get return value

The code is as follows Copy Code

$srv _ip = ' 192.168.1.5 ';//your target service address.

$srv _port = 80;//Port

$url = ' http://localhost/fsock.php '; Receive your post URL specific address

$fp = ";

$errno = 0;//error Handling

$ERRSTR = ";//error handling

$timeout = 10;//How long it has not been connected to the interruption

$post _str = "Username=demo&password=hahaha";//content to be submitted.

Open the Socket link for your 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 file pointer is to the end of the file location

$line = fgets ($fp, 1024);

Remove the header information from the request package

if ($inheader && ($line = = "N" | | $line = = "rn") {

$inheader = 0;

}

if ($inheader = = 0) {

Echo $line;

}

}

Fclose ($FP);

Unset ($line);

?>

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

The code is as follows Copy Code

echo "Username:". $_post[' username ']. "
";

echo "Password:". $_post[' password ');

?>


The result is:

Username:demo

Password:hahaha


Forge get


At the same time forge the Post,get method.

The code is as follows Copy Code

Fsocket Analog Post Submission
$purl = "Http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
Print_r (Parse_url ($url));
Sock_post ($purl, "uu=55555555555555555");
Fsocket Analog Get Commit
function Sock_get ($url, $query)
{
$info = Parse_url ($url);
$fp = Fsockopen ($info ["host"], $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=rrrrrrrrrrrrrrrr");
function Sock_post ($url, $query)
{
$info = Parse_url ($url);
$fp = Fsockopen ($info ["host"], $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;
}
}
?>


http://www.bkjia.com/PHPjc/444597.html www.bkjia.com true http://www.bkjia.com/PHPjc/444597.html techarticle in PHP fsockopen function can imitate the user to visit some websites and also can bring some commonly used information, if the browser, Ip,post,get and so on data, below I separately to everybody ...

  • Related Article

    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.