php模拟表单提交

用curl来模拟表单的提交

submit.php

<?php

header("content-type:text/html;charset=utf-8");

$title="php程序设计";

$content="php内容";

$url='http://localhost:8088/login/curl/form/data.php';

$post_fields='title='.$title.'&content='.$content;

$ch = curl_init($url);//开启一个crul会话

curl_setopt($ch, CURLOPT_HEADER, 0);//不包含头部

//curl_setopt($ch, CURLOPT_COOKIE, $cookie);//设置cookies

curl_setopt($ch,CURLOPT_REFERER,'http://localhost:8088/login/curl/form/submit.php');

        $r = rand(1,255);  

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.'.$r, 'CLIENT-IP:125.8.8.'.$r));  //构造IP

        curl_setopt($ch, CURLOPT_POST, 1);//允许接收post数据

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);  //设置post数据

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//输出内容为字符串

$content = curl_exec($ch);//得到curl结果

print_r($content);

curl_close($ch);//关闭curl会话

?>

data.php
 
<?php
var_dump($_POST);
var_dump($_SERVER);
echo $_SERVER["HTTP_REFERER"];
echo "<br>";
function getClientIp() {  
    if (!empty($_SERVER["HTTP_CLIENT_IP"]))  
        $ip = $_SERVER["HTTP_CLIENT_IP"];  
    else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))  
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
    else if (!empty($_SERVER["REMOTE_ADDR"]))  
        $ip = $_SERVER["REMOTE_ADDR"];  
    else  
        $ip = "err";  
    return $ip;  
}  
echo "IP: " . getClientIp() . "";  
?>
 
 
另一种写法
submit.php
 

 <?php

header("content-type:text/html;charset=utf-8");

$fields = array(  

'sitename'=>'phpjx' ,  

'siteurl'=>'http://www.phpjx.com' ,  

);  

$url='http://localhost:8088/login/curl/form/data.php';

$ch = curl_init($url);//开启一个crul会话

curl_setopt($ch,CURLOPT_REFERER,'www.phpjx.com');//curl伪造referer

$r = rand(1,255);  

$header[]= 'Host:www.phpjx.com';  

$header[]= 'Cookie: username=test;password=1*****u';//curl伪造cookie

$header[]='X-FORWARDED-FOR:8.8.8.'.$r;//curl伪造IP

$header[]='CLIENT-IP:125.8.8.'.$r;//curl伪造IP

curl_setopt($ch,CURLOPT_HTTPHEADER,$header);  

curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。  

curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名  

$content = curl_exec($ch);//得到curl结果

print_r($content);

curl_close($ch);//关闭curl会话

data.php
 
<?php
var_dump($_POST);
var_dump($_SERVER);
echo $_SERVER["HTTP_REFERER"];
echo "<br>";
function getClientIp() {  
    if (!empty($_SERVER["HTTP_CLIENT_IP"]))  
        $ip = $_SERVER["HTTP_CLIENT_IP"];  
    else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))  
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
    else if (!empty($_SERVER["REMOTE_ADDR"]))  
        $ip = $_SERVER["REMOTE_ADDR"];  
    else  
        $ip = "err";  
    return $ip;  
}  
echo "IP: " . getClientIp() . "";  
    A+
发布日期:2014年01月08日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: