欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php模拟post行为代码总结(POST方式不是绝对安全)

程序员文章站 2022-05-14 12:52:59
这里提供两种方法供选择:第一:手写代码。第二:利用httpclient php类库   第一种方法: 复制代码 代码如下: <?php $flag = 0; //要p...
这里提供两种方法供选择:第一:手写代码。第二:利用httpclient php类库
  第一种方法:
复制代码 代码如下:

<?php
$flag = 0;
//要post的数据
$argv = array(
'var1'=>'abc',
'var2'=>'你好吗');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$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";
//添加post的字符串
$header .= $params."";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "n" || $line == "")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>

  第二种方法是:使用httpclient类
复制代码 代码如下:

$pagecontents = httpclient::quickpost('http://example.com/someform', array(
'name' => 'some name',
'email' => 'email@example.com'
));

  使用httpclient类库,可以去官方下载最新的类库,官方地址为:http://scripts.incutio.com/httpclient/index.php
  附加一些点php httpclient的其他几个用法
  静态方法获取网页:
复制代码 代码如下:

$pagecontents = httpclient::quickget('http://bankcha.com')

  get方法获取
复制代码 代码如下:

$client = new httpclient('bankcha.com');
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
$pagecontents = $client->getcontent();
  带调试的get方法获取
  php代码
$client = new httpclient('bankcha.com');
$client->setdebug(true);
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
$pagecontents = $client->getcontent();
  带自动转向的get方法
  php代码
  $client = new httpclient('www.bankcha.com');
$client->setdebug(true);
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
$pagecontents = $client->getcontent();
  检查页面是否存在
  php代码
$client = new httpclient('bankcha.com');
$client->setdebug(true);
if (!$client->get('/thispagedoesnotexist')) {
die('an error occurred: '.$client->geterror());
}
if ($client->getstatus() == '404') {
echo 'page does not exist!';
}
$pagecontents = $client->getcontent();
  伪造客户端
  php代码
$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/20021207');
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
$pagecontents = $client->getcontent();
  登录验证并请求一个网页
  php代码
$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授权
  php代码
$client = new httpclient('bankcha.com');
$client->setauthorization('username', 'password');
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
$pagecontents = $client->getcontent();
  输出头信息
  php代码
$client = new httpclient('bankcha.com');
if (!$client->get('/')) {
die('an error occurred: '.$client->geterror());
}
print_r($client->getheaders());
  设置一个域内重定向最多次数
  php代码
$client = new httpclient('www.bankcha.com');
$client->setdebug(true);
$client->setmaxredirects(3);
$client->get('/');

php fsockopen 伪造 post和get方法
fsockopen 伪造 post和get方法哦,如果你正在找 伪造 post和get方法的php处理代码这款不错哦。
复制代码 代码如下:

<?php
//fsocket模拟post提交
$purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
print_r(parse_url($url));
sock_post($purl,"uu=55555555555555555");
//fsocket模拟get提交
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=rrrrrrrrrrrrrrrr");
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;
}
}
?>