php simulation post behavior code summary of POST way is not absolutely safe)

  • 2020-05-12 02:24:35
  • OfStack

There are two methods to choose from: 1: handwritten code. Step 2: leverage the HttpClient php class library
Method 1:
 
< ? PHP 
$flag = 0; 
// to post The data of  
$argv = array( 
'var1'=>'abc', 
'var2'=>' how are you doing '); 
// Structure to post The string  
foreach ($argv as $key=>$value) { 
if ($flag!=0) { 
$params .= "&"; 
$flag = 1; 
} 
$params.= $key."="; $params.= urlencode($value); 
$flag = 1; 
} 
$length = strlen($params); 
// create socket The connection  
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno); 
// structure post The head of the request  
$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"; 
// add post The string  
$header .= $params.""; 
// send post The data of  
fputs($fp,$header); 
$inheader = 1; 
while (!feof($fp)) { 
$line = fgets($fp,1024); // Remove the header from the request package to display only the returned data of the page  
if ($inheader && ($line == "n" || $line == "")) { 
$inheader = 0; 
} 
if ($inheader == 0) { 
echo $line; 
} 
} 
fclose($fp); 
?> 

The second method is to use the httpclient class
 
$pageContents = HttpClient::quickPost('http://example.com/someForm', array( 
'name' => 'Some Name', 
'email' => 'email@example.com' 
)); 

Use httpclient class library, can go to download the latest official class library, is the official address: http: / / scripts incutio. com/httpclient/index php
Attach 1 point to several other USES of php httpclient
Static method to get web pages:
 
$pageContents = HttpClient::quickGet('http://bankcha.com') 

The Get method
 
$client = new HttpClient('bankcha.com'); 
if (!$client->get('/')) { 
die('An error occurred: '.$client->getError()); 
} 
$pageContents = $client->getContent(); 
 With debugging Get Methods to obtain  
  PHP code  
$client = new HttpClient('bankcha.com'); 
$client->setDebug(true); 
if (!$client->get('/')) { 
die('An error occurred: '.$client->getError()); 
} 
$pageContents = $client->getContent(); 
 With automatic steering Get methods  
  PHP code  
  $client = new HttpClient('www.bankcha.com'); 
$client->setDebug(true); 
if (!$client->get('/')) { 
die('An error occurred: '.$client->getError()); 
} 
$pageContents = $client->getContent(); 
 Check to see if the page exists  
  PHP code  
$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(); 
 Fake client  
  PHP code  
$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(); 
 Login authentication and request 1 A web page  
  PHP code  
$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 authorization  
  PHP code  
$client = new HttpClient('bankcha.com'); 
$client->setAuthorization('Username', 'Password'); 
if (!$client->get('/')) { 
die('An error occurred: '.$client->getError()); 
} 
$pageContents = $client->getContent(); 
 Output header information  
  PHP code  
$client = new HttpClient('bankcha.com'); 
if (!$client->get('/')) { 
die('An error occurred: '.$client->getError()); 
} 
print_r($client->getHeaders()); 
 Set up the 1 The maximum number of redirects in each field  
  PHP code  
$client = new HttpClient('www.bankcha.com'); 
$client->setDebug(true); 
$client->setMaxRedirects(3); 
$client->get('/'); 

php fsockopen forged post and get methods
fsockopen forgery post and get methods oh, if you're looking for php processing code forgery post and get methods this is great.
 
<?php 
//fsocket simulation post submit  
$purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr"; 
print_r(parse_url($url)); 
sock_post($purl,"uu=55555555555555555"); 
//fsocket simulation get submit  
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; 
} 
} 
?> 

Related articles: