PHP Fsockopen Imitation post and get detailed

Source: Internet
Author: User
Tags error handling fread http request strlen trim

If you want to use the Fsockopen function we must set the Allow_url_fopen = on in the php.ini to the open state.

Cases

Fsockopen () Example

  code is as follows copy code

<?php
$fp = Fsockopen ("www.example.com", $errno, $errstr, 30);
if (! $fp) {
    echo $errstr ($errno) <br/>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);
& nbsp;  }
    fclose ($fp);
}
?


Forged Post

POST http request (URL) and get the return value

The code is as follows Copy Code

<?php

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

$srv _port = 80;//Port

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

$fp = ';

$errno = 0;//error Handling

$errstr = ';//error handling

$timeout = How long has 10;//been interrupted without a connection?

$post _str = "Username=demo&password=hahaha";//what to submit.

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

$line = fgets ($fp, 1024);

Remove 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 the fsock.php,fsock.php content as follows:

The code is as follows Copy Code

<?php

echo "Username:". $_post[' username '. " <br/> ";

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

?>


The results are:

Username:demo

Password:hahaha


Fake get


At the same time forge Post,get method.

The code is as follows Copy Code

<?php
Fsocket Analog Post Submission
$purl = "Http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
Print_r (Parse_url ($url));
Sock_post ($purl, "uu=55555555555555555");
Fsocket Simulate 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;
}
}
?>

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.