Home >Backend Development >PHP Tutorial >The perfect cracking method to prevent hotlinking of online photo album pictures implemented in PHP_PHP Tutorial

The perfect cracking method to prevent hotlinking of online photo album pictures implemented in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:47:531204browse

The perfect cracking method to prevent hotlinking of online album pictures implemented in php

This article describes the perfect cracking method to prevent hotlinking of online album pictures implemented in php. Share it with everyone for your reference. The details are as follows:

Internet photo album picture anti-hotlink cracking program - PHP version. This anti-hotlink cracking version can perfectly crack the currently popular: Baidu Photo Album, NetEase Photo Album, 360 I Like and other website pictures. It can also implement simple picture anti-hotlinking. Because of this The class first obtains the remote image, and then sends the image to the client, so it is regarded as transmitting traffic twice. Therefore, space traffic will be wasted. Next, the cache function will be developed, which can save traffic!

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

/**

* Internet photo album picture anti-leeching cracking program - PHP version

*

* How to use:

*

* http://yourdomain/url.php?url=http://hiphotos.baidu.com/verdana/pic/item/baidupicture.jpg&referer=

* The url refers to the image URL that needs to be cracked, and the referer is to be compatible with some photo albums that do not require setting the source domain name to be displayed, such as 360 I Like Network, which must be set to empty to browse normally. Therefore, this Referer should be set to 1

*

* @author Snow Fox Blog

* @version 1.0

* @since July 16, 2012

* @URL http://www.xuehuwang.com

*/

class Frivoller

{

/**

* HTTP version number (1.0, 1.1), Baidu uses version 1.1

*

* @var string

*/

protected $version;

/**

* Response data after making HTTP request

*

* @var string format

*/

protected $body;

/**

* Remote URL to be obtained

*

* @var string format

*/

protected $link;

/**

* An array that containing any of the various components of the URL.

*

* @var array

*/

protected $components;

/**

* HOST data during HTTP request

*

* @var string

*/

protected $host;

/**

* The path of required file.

* (e.g. '/verdana/abpic/item/mygirl.png')

*

* @var string

*/

protected $path;

/**

* The HTTP referer, extra it from original URL

*

* @var string

*/

protected $referer;

/**

* The HTTP method, 'GET' for default

*

* @var string

*/

protected $method = 'GET';

/**

* The HTTP port, 80 for default

*

* @var int

*/

protected $port = 80;

/**

* Timeout period on a stream

*

* @var int

*/

protected $timeout = 100;

/**

* The filename of image

*

* @var string

*/

protected $filename;

/**

* The ContentType of image file.

* image/jpeg, image/gif, image/png, image

*

* @var string

*/

protected $contentType;

/**

* Frivoller constructor

*

* @param string $link

*/

public function __construct($link,$referer='')

{

$this->referer = $referer;

// parse the http link

$this->parseLink($link);

// begin to fetch the image

$stream = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);

if (!$stream){

header("Content-Type: $this->contentType;");

echo $this->CurlGet($link);

}else{

fwrite($stream, $this->buildHeaders());

$this->body = "";

$img_size = get_headers($link,true);

while (!feof($stream)) {

$this->body .= fgets($stream, $img_size['Content-Length']);

//fwrite($jpg,fread($stream, $img_size['Content-Length']));

}

$content = explode("rnrn", $this->body, 2);

$this->body = $content[1];

fclose($stream);

// send 'ContentType' header for saving this file correctly

// 如果不发送CT,则在试图保存图片时,IE7 会发生错误 (800700de)

// Flock, Firefox 则没有这个问题,Opera 没有测试

header("Content-Type: $this->contentType;");

header("Cache-Control: max-age=315360000");

echo $this->body;

//保存图片

//file_put_contents('hello.jpg', $this->body);

}

}

/**

* Compose HTTP request header

*

* @return string

*/

private function buildHeaders()

{

$request = "$this->method $this->path HTTP/1.1rn";

$request .= "Host: $this->hostrn";

$request .= "Accept-Encoding: gzip, deflatern";

$request .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1rn";

$request .= "Content-Type: image/jpegrn";

$request .= "Accept: */*rn";

$request .= "Keep-Alive: 300rn";

$request .= "Referer: $this->refererrn";

$request .= "Cache-Control: max-age=315360000rn";

$request .= "Connection: closernrn";

return $request;

}

/**

* Strip initial header and filesize info

*/

private function extractBody(&$body)

{

// The status of link

if(strpos($body, '200 OK') > 0) {

// strip header

$endpos = strpos($body, "rnrn");

$body = substr($body, $endpos 4);

// strip filesize at nextline

$body = substr($body, strpos($body, "rn") 2);

}

}

/**

* Extra the http url

*

* @param $link

*/

private function parseLink($link)

{

$this->link = $link;

$this->components = parse_url($this->link);

$this->host = $this->components['host'];

$this->path = $this->components['path'];

if(empty($this->referer)){

$this->referer = $this->components['scheme'] . '://' . $this->components['host'];

}elseif($this->referer == '1'){

$this->referer = '';

}

$this->filename = basename($this->path);

// extract the content type

$ext = substr(strrchr($this->path, '.'), 1);

if ($ext == 'jpg' or $ext == 'jpeg') {

$this->contentType = 'image/pjpeg';

}

elseif ($ext == 'gif') {

$this->contentType = 'image/gif';

}

elseif ($ext == 'png') {

$this->contentType = 'image/x-png';

}

elseif ($ext == 'bmp') {

$this->contentType = 'image/bmp';

}

else {

$this->contentType = 'application/octet-stream';

}

}

//抓取网页内容

function CurlGet($url){

$url = str_replace('&','&',$url);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_REFERER,$url);

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");

curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');

curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);

$values = curl_exec($curl);

curl_close($curl);

return $values;

}

}

/**

* Obtain root domain name

*

* @author lonely

* @create 2011-3-11

* @version 0.11

* @lastupdate lonely

* @package Sl

*/

class RootDomain{

private static $self;

private $domain=null;

private $host=null;

private $state_domain;

private $top_domain;

/**

* Obtain domain name analysis example

* Enter description here ...

*/

public static function instace(){

if(!self::$self)

self::$self=new self();

return self::$self;

}

public function __construct(){

$this->state_domain=array(

'al','dz','af','ar','ae','aw','om','az','eg','et','ie','ee','ad','ao','ai','ag','at','au','mo','bb','pg','bs','pk','py','ps','bh','pa','br','by','bm',

'bg','mp','bj','be','is','pr','ba','pl','bo','bz','bw','bt','bf','bi','bv','kp','gq','dk','de','tl','tp','tg','dm','do','ru','ec','er','fr','fo','pf','gf','tf','v

a','ph','fj','fi','cv','fk','gm','cg','cd','co','cr','gg','gd','gl','ge','cu','gp','gu','gy','kz','ht','kr','nl','an','hm','hn','ki','dj','kg','gn','gw','ca'

,'gh','ga','kh','cz','zw','cm','qa','ky','km','ci','kw','cc','hr','ke','ck','lv','ls','la','lb','lt','lr','ly','li','re','lu','rw','ro','mg','im','mv','mt','mw

','my','ml','mk','mh','mq','yt','mu','mr','us','um','as','vi','mn','ms','bd','pe','fm','mm','md','ma','mc','mz','mx','nr','np','ni','ne','

ng','nu','no','nf','na','za','aq','gs','eu','pw','pn','pt','jp','se','ch','sv','ws','yu','sl','sn','cy','sc','sa','cx','st','sh','kn','lc','sm','pm','vc

','lk','sk','si','sj','sz','sd','sr','sb','so','tj','tw','th','tz','to','tc','tt','tn','tv','tr','tm','tk','wf','vu','gt','ve','bn','ug','ua','uy','uz','es','eh','gr','hk

','sg','nc','nz','hu','sy','jm','am','ac','ye','iq','ir','il','it','in','id','uk','vg','io','jo','vn','zm','je','td','gi','cl','cf','cn','yr'

);

$this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','m

e','mobi');

$this->url=$_SERVER['HTTP_HOST'];

}

/**

* 设置URL

* Enter description here ...

* @param string $url

*/

public function setUrl($url=null){

$url=$url?$url:$this->url;

if(empty($url))return $this;

if(!preg_match("/^http:/is", $url))

$url="http://".$url;

$url=parse_url(strtolower($url));

$urlarr=explode(".", $url['host']);

$count=count($urlarr);

if ($count<=2){

$this->domain=$url['host'];

}else if ($count>2){

$last=array_pop($urlarr);

$last_1=array_pop($urlarr);

if(in_array($last, $this->top_domain)){

$this->domain=$last_1.'.'.$last;

$this->host=implode('.', $urlarr);

}else if (in_array($last, $this->state_domain)){

$last_2=array_pop($urlarr);

if(in_array($last_1, $this->top_domain)){

$this->domain=$last_2.'.'.$last_1.'.'.$last;

$this->host=implode('.', $urlarr);

}else{

$this->host=implode('.', $urlarr).$last_2;

$this->domain=$last_1.'.'.$last;

}

}

}

return $this;

}

/**

* Obtain domain name

* Enter description here ...

*/

public function getDomain(){

return $this->domain;

}

/**

* Obtain host

* Enter description here ...

*/

public function getHost(){

return $this->host;

}

}

$referer = array('xuehuwang.com','zangbala.cn','qianzhebaikou.net','sinaapp.com','163.com','sina.com.cn','weibo.com','abc.com');

// Get the url, maybe you should check the given url

if (isset($_GET['url']) and $_GET['url'] != '') {

//获取来路域名

$site = (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';

//匹配是否是一个图片链接

if(preg_match('/(http|https|ftp|rtsp|mms):(//|\\){1}((w) [.]){1,}([a-zA-Z]|[0-9]{1,3})(S*/)((S) [.]{1}(gif|jpg|png|bmp))/i',$_GET['url'])){

if(!empty($site)){

$tempu = parse_url($site);

$host = $tempu['host'];

$root = new RootDomain();

$root->setUrl($site);

if(in_array($root->getDomain(),$referer)){

$img_referer = (isset($_GET['referer']) && !empty($_GET['referer']))? trim($_GET['referer']) : '';

new Frivoller($_GET['url'],$img_referer);

}

}else{

$img_referer = (isset($_GET['referer']) && !empty($_GET['referer']))? trim($_GET['referer']) : '';

new Frivoller($_GET['url'],$img_referer);

}

}

}

?>

  希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1025323.htmlTechArticlephp实现的网络相册图片防盗链完美破解方法 本文实例讲述了php实现的网络相册图片防盗链完美破解方法。分享给大家供大家参考。具体如下...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn