PHP+socket发送请求文件
xx.php通过代理、路由器打开一条通道访问客户端
-
连接某URL的80端口(打开)
-
发送头信息(写)
-
读取网页内容(读)
GET发送
<?php
/*
PHP+socket编程 发送请求
要求能 模拟下载、注册、登陆、批量发帖
*/
//http请求类的接口
interface proto{
//连接url
function conn($url);
//发送get查询
function get();
//发送post查询
function post();
//关闭连接
function close();
}
Class Http implements Proto{
const CRLF ="\r\n";
protected $erron=-1;
protected $errstr='';
protected $response='';
protected $url=null;
protected $version='HTTP/1.1';
protected $fh=null;
protected $line=array();
protected $header=array();
protected $body=array();
public function _construct($url){
$this->conn($url);
$this->setHeader('Host:' .$this->url['host'] );
}
//此方法负责写请求行
protected function setLine($method){
$this->Line[0]=$method . '' . $this->url['path'] . ' ' . $this->version;
}
//此方法负责写头信息
protected function setHeader($hederline){
$this->header[]=$headerline;
}
//此方法负责写主体信息
protected function setBody(){
}
//连接url
public function conn($url){
$this->url= parse_url($url);
//判断端口
if(isset($this->url['port'])){
$this->url['port']=80;
}
$this->fh= fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
}
//构造get请求的数据
public function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//构造post查询的数据
public function post(){
}
//真正请求
public function request(){
//把请求行,头信息,实体信息,放在一个数组中,便于拼接
$req=array_merge($this->line,$this-
>header,array(''),$this->body,array(''));
//print_r($req);
$req=implode(self::CRLF,$req);
//echo $req;
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response.=fread($this->fh,1024);
}
$this->close();//关闭连接
return $this->response;
}
//关闭连接
function close(){
}
}
/*
$url='http://renjian.163.com/20/1019/11/FPA1LP5V000181RV.html';
$http=new Http($url);
echo $http->get();
*/
POST发送
<?php
/*
PHP+socket编程 发送请求
要求能 模拟下载、注册、登陆、批量发帖
*/
//http请求类的接口
interface proto{
//连接url
function conn($url);
//发送get查询
function get();
//发送post查询
function post();
//关闭连接
function close();
}
Class Http implements Proto{
const CRLF ="\r\n";
protected $erron=-1;
protected $errstr='';
protected $response='';
protected $url=null;
protected $version='HTTP/1.1';
protected $fh=null;
protected $line=array();
protected $header=array();
protected $body=array();
public function _construct($url){
$this->conn($url);
$this->setHeader('Host:' .$this->url['host'] );
}
//此方法负责写请求行
protected function setLine($method){
$this->Line[0]=$method . '' . $this->url['path'] . ' ' . $this->version;
}
//此方法负责写头信息
protected function setHeader($hederline){
$this->header[]=$headerline;
}
//此方法负责写主体信息
protected function setBody($boby){
$this->boby[]=http_build_query($body);
}
//连接url
public function conn($url){
$this->url= parse_url($url);
//判断端口
if(isset($this->url['port'])){
$this->url['port']=80;
}
$this->fh= fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
}
//构造get请求的数据
public function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//构造post查询的数据
public function post($boby=array()){
$this->setLine('POST');
//设计content-type
$this->setHeader('Content-type:application/x-www-form-urlencoded')
//设计主题信息,比GET不一样的地方
$this->setBoby($boby);
$this->request();
//计算content-length
$this->setHeader('Content-length:' . strlen($this->boby[0]))
}
//真正请求
public function request(){
//把请求行,头信息,实体信息,放在一个数组中,便于拼接
$req=array_merge($this->line,$this-
>header,array(''),$this->boby,array(''));
//print_r($req);
$req=implode(self::CRLF,$req);
//echo $req;exit;
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response.=fread($this->fh,1024);
}
$this->close();//关闭连接
return $this->response;
}
//关闭连接
public function close(){
fclose($this->fh);
}
}
/*
$url='http://renjian.163.com/20/1019/11/FPA1LP5V000181RV.html';
$http=new Http($url);
echo $http->get();
*/
set_time_limit(0);
$url='http://renjian.163.com/20/1019/11/FPA1LP5V000181RV.html';
for($i=1;$i<100;$i++){
$str=str_shuffle'abcdfghijkimnopqrst0776656';
$tit=substr($str,0,5);
$con=substr($str,6,8);
$http=new Http($url);
echo $http->post(array('tit'=>$tit,'con'=>$con,'submit'=>'留言'));
echo $tit,'-----------',$con,'<br />';
usleep(2000);
}
无状态?
2次请求之间没有关系
?服务器如何记住一个客户?
cookie 模拟登录发贴,cookie可以记住用户
<?php
require('./http.class.php')
$http=new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0');
$http->setHeader('Referer: ');
$http->setHeader('User-Agent: ');
$http->setHeader('cookie: ');
$msg =array(
'formhash'=>'4f23e777',
'message'=>'world',
'pmsubmit'=>'true',
'pmsubmit_btn'=>'发送',
'refer'=>'http://home.verycd.com/space.php?do=pm&filter=privatepm',
'username'=>'http接收'
);
file_put_contents('./res.htlm' $http->post($msg ));
echo '0k';
HTTP协议之refer防盗链
图片经允许不能引用
???服务器是怎样知道这张图片是在站外被引用的呢?
还有网站的统计结果里,网站的用户的来源?
???统计时,是如何得知用户时从哪里来的网站
在Http协议中,头信息里有一个重要的选项:Referer
Referer:代表网页的来源,即上一页的地址
如果直接在浏览器上输入地址进来则没有Referer选项
这也是:为什么服务器知道我们的图片是从哪引用的,也知道我们的客户是从哪个网站连接点击过来的.
问题:如何配置apache服务器,用于图片防盗链?
原理:在web服务器层面,根据http协议referer头信息来判断.
如果来自站外,则统一重写到一个很小的防盗链图片上去
具体步骤:1:打开apache重写模块 mod_rewrite
2:在需要防盗的网站或目录下,写htaccess文件,
并指定防盗链规则
如何指定?
自然是分析referer信息,如果不是来自本站,是重写
重写规则:
哪种情况重写:
是jpep/jpg/gif/png图片时
是referer头与localhost不匹配时
重写
怎么重写?
统一rewriete到某个防盗链图片
如下面的例子:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*\.(jpg|jpeg|gif|png)[NC]
RewriteCond %{HTTP_REFERER} !localhost [NC]
RewriteRule .* no.png
<?php
require('.http.class.php');
$http=new Http('http://localhost/1020/apple.jpg');
$http->setHeader('Referer: http://localhost')
$res= $http->get()
file_put_contents('./apple.jpg',substr(strstr($res,"\r\n\r\n"),4));
echo'ok';
//此程序有不完善的地方----应该判断路径或response的mime头信息,确认图片类型
eg|gif|png)[NC]
RewriteCond %{HTTP_REFERER} !localhost [NC]
RewriteRule .* no.png
```php
<?php
require('.http.class.php');
$http=new Http('http://localhost/1020/apple.jpg');
$http->setHeader('Referer: http://localhost')
$res= $http->get()
file_put_contents('./apple.jpg',substr(strstr($res,"\r\n\r\n"),4));
echo'ok';
//此程序有不完善的地方----应该判断路径或response的mime头信息,确认图片类型