Demo
原理
从保存有图片地址的txt中随机选择一个图片地址,并返回出此图片。
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 |
<?php $filename = 'img_path.txt'; if(!file_exists($filename)){ die('文件不存在');// 输出信息并退出脚本 } // 使用数组存放链接 $pics = []; $fs = fopen($filename, 'r'); while(!feof($fs)){// feof:检测指针是否达到文件末尾 $line = trim(fgets($fs));// fgets:从文件指针中读取一行 if($line != ''){ array_push($pics, $line); } } // 从数组中随机选取一个链接 $pic = $pics[array_rand($pics)]; /* // 以JSON形式返回 header('Content-Type:text/json'); die(json_encode(['pic'=>$pic])); */ //根据地址查看文件后缀名称echo(pathinfo($pic, PATHINFO_EXTENSION)); header('Content-type:image/webp'); $handle = fopen($pic,'r'); $res = fread($handle, filesize($pic)); fclose($handle); die($res); ?> |
添加IP访问频率限制
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 |
/* *通过禁止IP频繁访问防止网站被防攻击代码 *design by www.scutephp.com */ header('Content-type: text/html; charset=utf-8'); $ip=$_SERVER['REMOTE_ADDR'];//获取当前访问者的ip $logFilePath='./log/';//日志记录文件保存目录 $fileht='.htaccess2';//被禁止的ip记录文件 $allowtime=60;//防刷新时间 $allownum=20;//防刷新次数 $allowRefresh=999;//在允许刷新次数之后加入禁止ip文件中 if(!file_exists($fileht)){ file_put_contents($fileht,''); } $filehtarr=@file($fileht); if(in_array($ip."\r\n",$filehtarr)){ exit('警告:操作频繁!请稍后再试。'); } //加入禁止ip $time=time(); $fileforbid=$logFilePath.'forbidchk.dat'; if(file_exists($fileforbid)){ if($time-filemtime($fileforbid)>30){ @unlink($fileforbid); }else{ $fileforbidarr=@file($fileforbid); if($ip==substr($fileforbidarr[0],0,strlen($ip))){ if($time-substr($fileforbidarr[1],0,strlen($time))>120){ @unlink($fileforbid); }else if($fileforbidarr[2]>$allowRefresh){ file_put_contents($fileht,$ip."\r\n",FILE_APPEND); @unlink($fileforbid); }else{ $fileforbidarr[2]++; file_put_contents($fileforbid,$fileforbidarr); } } } } //防刷新 $str=''; $file=$logFilePath.'ipdate.dat'; if(!file_exists($logFilePath)&&!is_dir($logFilePath)){ mkdir($logFilePath,0777); } if(!file_exists($file)){ file_put_contents($file,''); } $uri=$_SERVER['REQUEST_URI'];//获取当前访问的网页文件地址 $checkip=md5($ip); $checkuri=md5($uri); $yesno=true; $ipdate=@file($file); foreach($ipdate as $k=>$v){ $iptem=substr($v,0,32); $uritem=substr($v,32,32); $timetem=substr($v,64,10); $numtem=substr($v,74); if($time-$timetem<$allowtime){ if($iptem!=$checkip){ $str.=$v; }else{ $yesno=false; if($uritem!=$checkuri){ $str.=$iptem.$checkuri.$time."\r\n"; }else if($numtem<$allownum){ $str.=$iptem.$uritem.$timetem.($numtem+1)."\r\n"; } else{ if(!file_exists($fileforbid)){ $addforbidarr=array($ip."\r\n",time()."\r\n",1); file_put_contents($fileforbid,$addforbidarr); } file_put_contents($logFilePath.'forbided_ip.log',$ip.'--'.date('Y-m-d H:i:s',time()).'--'.$uri."\r\n",FILE_APPEND); $timepass=$timetem+$allowtime-$time; exit('警告:不要刷新的太频繁哦!'); } } } } if($yesno){ $str.=$checkip.$checkuri.$time."\r\n"; } file_put_contents($file,$str); |
利用python批量更新图片地址信息
在使用时可以将所有图片保存在同一文件夹中,然后利用python批量更新图片地址信息。
1 2 3 4 5 6 7 8 9 10 |
import os path = r'xxx\images' # 图片所在文件夹 img_name = os.listdir(path) with open(r'xxx\img_path.txt','w') as f: for i in img_name[:-1]: f.writelines('images\\'+i+'\n') f.writelines('images\\'+img_name[-1]) |
暂无评论