image.php代码部分:(请忽略博客代码类型标识的错误)
<?php
$dir = './images/';
$list = scandir($dir,0);
$rand = rand(2,count($list)-'1');
$file = $dir.$list[$rand];
header("Location:{$file}");
/**
//方法2,直接输出
$info = @getimagesize($file);
$mime = $info["mime"];
if($mime == "image/png")
{
header("Content-Type:image/png");
}
elseif($mime == "image/jpeg")
{
header("Content-Type:image/jpeg");
}
elseif($mime == "image/gif")
{
header("Content-Type:image/gif");
}
elseif($mime == "image/vnd.wap.wbmp")
{
header("Content-Type:image/vnd.wap.wbmp");
}
elseif($mime == "image/x-xbitmap")
{
header("Content-Type:image/x-xbitmap");
}
elseif($mime == "image/webp")
{
header("Content-Type:image/webp");
}
elseif($mime == "image/bmp")
{
header("Content-Type:image/bmp");
}
else
{
header("Content-Type:image/png");
}
echo file_get_contents($file);
*/
?>
1.去方法2和结束标识'?>'
<?php
$dir = './images/';
$list = scandir($dir,0);
$rand = rand(2,count($list)-'1');
$file = $dir.$list[$rand];
header("Location:{$file}");
2.去变量
去变量$dir
<?php
$list = scandir('./images/',0);
$rand = rand(2,count($list)-'1');
$file = './images/'.$list[$rand];
header("Location:{$file}");
去变量$file
<?php
$list = scandir('./images/',0);
$rand = rand(2,count($list)-'1');
header('Location:./images/'.$list[$rand]);
去变量$rand
<?php
$list = scandir('./images/',0);
header('Location:./images/'.$list[rand(2,count($list)-'1')]);
3.精简变量和函数(省略换行,'<?php'后紧跟' '(空格))
<?php $l = scandir('./images/',0);header('Location:./images/'.$l[rand(2,count($l)-'1')]);
合并$l到header(不推荐),会导致遍历两遍目录(两倍的时间花费)
<?php header('Location:./images/'.scandir('./images/')[rand(2,count(scandir('./images/'))-'1')]);
最终结果:
<?php $l=scandir('./images/');header('Location:./images/'.$l[rand(2,count($l)-'1')]);
共只有1行,第一行开始是PHP标签和变量$l,遍历目录,然后是重定向,复合了数组抽取随机键(数组的键0和1的值分别是'.'和'..',因此随机数从2开始,而最大值-'1'(减1)是因为数组是从0开始的)
相关项目:
Comments 15 条评论
芜湖
Arx Nimbus’ Thrivaca Risk Analytics Platform provides the first financially-relevant Digital Risk Profile for the enterprise, underwriters and brokers. Using a patented algorithm developed in collaboration with leading universities, the DoD and federal regulators, Thrivaca consolidates industry historical analysis, your latest analyses and assessments, proprietary vulnerability scans, audit data and actuarial mathematics to produce a complete and detailed valuation of enterprise-wide digital risk, and the top sources of current carrying cost. Jutta Glidewell
I loved your article.Really looking forward to read more. Really Cool. Joycelyn Mioduszewski
Very good blog article.Really looking forward to read more. Really Cool. Alta Lanni
I’ll immediately grasp your rss feed as I can’t in finding your email subscription hyperlink or e-newsletter service. Do you’ve any? Please allow me understand in order that I may just subscribe. Thanks. Penney Grund
Thanks to my father who shared with me about this website,
this web site is really amazing.
Very nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed browsing your blog posts.
After all I’ll be subscribing to your rss feed and I hope you write again very soon!
Hi fantastic website! Does running a blog like this require a massive amount work? I have virtually no knowledge of coding however I was hoping to start my own blog in the near future. Anyhow, if you have any suggestions or techniques for new blog owners please share. I understand this is off topic however I simply needed to ask. Many thanks! Ned Tures
I visited many websites except the audio quality for audio songs existing at this site is truly superb. Ethel Link Melania
Very interesting topic, appreciate it for putting up. Loretta Brendin Glanti
I think this web site has very superb written articles articles . Dorry Reube Graner
Wonderful website. Lots of useful information here. Gnni Tades Weissberg
I every time emailed this blog post page to all my contacts, as if like to read it then my friends will too. Kissee Kalvin Marja
If you want to use the photo it would also be good to check with the artist beforehand in case it is subject to copyright. Best wishes. Aaren Reggis Sela