Day46

PHP+socket发送请求文件

xx.php通过代理、路由器打开一条通道访问客户端

  1. 连接某URL的80端口(打开)

  2. 发送头信息(写)

  3. 读取网页内容(读)

    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头信息,确认图片类型

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值