更新 'index.php'

This commit is contained in:
rainerosion 2021-04-10 20:21:36 +08:00
parent 3bb42a5ba4
commit d1d1f65713

View File

@ -1,17 +1,27 @@
<?php <?php
error_reporting(0); error_reporting(0);
$file_path = "data/images.json"; $file_path = "data/images.json";
if(file_exists($file_path)){ $memcache = new Memcache;
$fp = fopen($file_path,"r"); $memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$str = fread($fp,filesize($file_path)); $json = $memcache->get('randUrlJson');
$res = json_decode($str,true); // echo "获取缓存";
$randNum = rand(0,sizeof($res)-1); if(trim($json) == "" || $json == false){
$url = $res[$randNum]['url']; if(file_exists($file_path)){
$name = $res[$randNum]['name']; $fp = fopen($file_path,"r");
if($_GET['return'] == 'json'){ $str = fread($fp,filesize($file_path));
$arr = ['code' => 0,'url' => $url,'name' => $name]; $memcache->set('randUrlJson', $str);
echo json_encode($arr); $json = $str;
} else { // echo "设置缓存";
header('Location:'.$url); }
} }
$res = json_decode($json,true);
$randNum = rand(0,sizeof($res)-1);
$url = $res[$randNum]['url'];
$name = $res[$randNum]['name'];
if($_GET['return'] == 'json'){
$arr = ['code' => 0,'url' => $url,'name' => $name];
echo json_encode($arr);
} else {
// echo $url;
header('Location:'.$url);
} }