宝马娱乐在线城-宝马娱乐在线

来自 互联网络 2019-10-01 17:10 的文章
当前位置: 宝马娱乐在线城 > 互联网络 > 正文

php中fsockopen模仿post与get详解

 代码如下

如果你要使用fsockopen函数我们必须在php.ini中把allow_url_fopen = On 设置为开启状态。

<?php
function wfopen($url,$post='',$cookie='',$timeout=15) {
        $matches = parse_url($url);
        $out = "POST {$matches['path']} HTTP/1.0rn";
        $out .= "Accept: */*rn";
        $out .= "Accept-Language: zh-cnrn";
        $out .= "Content-Type: application/x-www-form-urlencodedrn";
        $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT] rn";
        $out .= "Host: {$matches['host']}rn";
        $out .= 'Content-Length: '.strlen($post)."rn";
        $out .= "Connection: Closern";
        $out .= "Cache-Control: no-cachern";
        $out .= "Cookie: $cookiernrn";
        $out .= $post;
        $socket = @fsockopen($matches['host'],80,$errno,$errstr,$timeout) or die("$errstr($errno)");
        fwrite($socket,$out);
        $header = $data = "";
        while($infos = trim(fgets($socket,4096))) {
                $header.=$infos;
        }
        while(!feof($socket)) {
                $data .= fgets($socket,4096);
        }
        return $data;
}
echo wfopen('http://localhost/te.php','id=5');
?>

te.php

 fsockopen() Example

<?php
if(!empty($_POST['id'])) {
echo setcookie('auth','haowei',time()+3600,'/') ? 1 : 0;
}

 代码如下

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

伪造post

POST HTTP请求(URL)并获取返回值

 代码如下

<?php 

  $srv_ip = '192.168.1.5';//你的目标服务地址. 

  $srv_port = 80;//端口 

  $url = 'http://localhost/fsock.php'; //接收你post的URL具体地址  

  $fp = ''; 

  $errno = 0;//错误处理 

  $errstr = '';//错误处理 

  $timeout = 10;//多久没有连上就中断 

  $post_str = "username=demo&password=hahaha";//要提交的内容. 

  //打开网络的 Socket 链接。 

  $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"; 

本文由宝马娱乐在线城发布于互联网络,转载请注明出处:php中fsockopen模仿post与get详解

关键词: