首页 > 解决方案 > 显示 .txt 文件中的随机文本,但每个 IP 地址仅显示一次

问题描述

这是我的 php 代码,用于检查 random.txt 文件中的文本并随机显示其中一个。

<?php
 //Path of file
 $myFile = "random.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

这是我的 random.txt 文件中的内容。

Jack
Binsky
Igor
Ivan
Eva

我希望我的代码仅向访问者显示此名称一次。这意味着如果有人从 IP 地址访问我的网页,该页面将随机显示 .txt 文件中的名称。但如果访问者重新加载网页,它不会第二次显示该 IP 地址的名称。

我怎样才能做到这一点?

标签: phphtmlrandom

解决方案


推荐阅读