2

我的PHP很差,但我正在努力改进!

我正在尝试编写一个非常简单的 php 脚本,该脚本从文本文件列表中加载随机 html 页面。

一旦人们查看了 html 页面,他们就会链接回 random.php 文件并加载另一个页面......这可以永远持续下去。

我正在使用文本文件列表,因为我会定期添加更多页面。我的问题是我的代码中没有地方可以防止重复访问!现在我只有大约 8 个链接,而且不止一次我有相同的链接“随机”连续出现 3 次 :( 希望我可以添加一些简单的东西来防止重复,如果已查看所有链接,然后重置。非常感谢 :)

    <body>

    <?php
    $urlist=file("randomlinks.txt");
    $nl=count($urlist);
    $np=rand(0,$nl-1);
    $url=trim($urlist[$np]);
    header("Location: $url");
    exit;
    ?>

    </body>
4

3 回答 3

2

Since the user does not know in what order the links are in the text file, if you were to read said links in sequence they would seem "random" (and you can shuffle them when first creating the file).

So you can:

  • save in session the index of the last link seen
  • link the link index to system time. This does not prevent repetitions, but guarantees that no two links come out equal, unless you hit 'refresh' after exactly the right amount of time.

Method 1:

$urlist=file("randomlinks.txt");
$nl=count($urlist);

session_start();
if (!isset($_SESSION['link'])) // If link is not in session
    $_SESSION['link'] = 0;    // Start from 0 (the first)
$np = $_SESSION['link']++;    // Next time will use next
$_SESSION['link'] %= $nl;     // Start over if nl exceeded

$url=trim($urlist[$np]);
Header("Location: $url");

Method 2:

...
$nl=count($urlist);
$np = time() % $nl;           // Get number of seconds since the Epoch,
                              // extract modulo $nl obtaining a number that
                              // cycles between 0 and $nl-1, every $nl seconds
$url=trim($urlist[$np]);
Header("Location: $url");

Another method would be to remember the last N links seen - but for this, you need a session variable - so as not to get them again too soon.

session_start();
if (!isset($_SESSION['urlist']))       // Do we know the user?
    $_SESSION['urlist'] = array();     // No, start with empty list
if (empty($_SESSION['urlist']))        // Is the list empty?
{
    $_SESSION['urlist'] = file("randomlinks.txt");   // Fill it.
    $safe = array_pop($_SESSION['urlist']);
    shuffle($_SESSION['urlist']);           // Shuffle the list
    array_push($_SESSION['urlist'], $safe);
}
$url = trim(array_pop($_SESSION['urlist']));

If you have five URLS 1, 2, 3, 4 and 5, you might get:

1 5 3 4 2 1 4 2 5 3 1 2 3 5 4 1 4 3 2 5 1 4 ...

...the list is N-1 random :-), all links appear with equal frequency, and the same link may reappear at most at a 2-remove, like the "4" above (...4 1 4...); if it does, you'll never see it again for at least $nl visits.

ALSO

  1. You should not use Header() from within a <BODY> tag. Remove <BODY> altogether.
  2. You don't need to use exit() if you are at the natural end of the script: the script will exit by itself.
于 2012-09-04T22:24:37.467 回答
1

我能想到的最简单的方法是使用 cookie。

网上到处都是这样的教程:http: //www.w3schools.com/php/php_cookies.asp

例如:

<?php
if (isset($_COOKIE["vistList"]))
  $visited = split(","$_COOKIE["visitList"]);
  foreach ($visited as &$value) {
    if ($value == /* new site url */) {
      //Find a new one
    }
  }
else
  $expire=time()+60*60*24*30;
  setcookie("vistList", "List-of-visited-URLs, separated-by-commas", $expire);
?>

我还没有机会测试这段代码,但希望它能给你一些想法。

正如评论中所指出的,同样的事情可以使用 php 会话来完成:

<?php
session_start();
if (isset($_SESSION["vistList"]))
  $visited = split(","$_SESSION["visitList"]);
  foreach ($visited as &$value) {
    if ($value == /* new site url */) {
      //Find a new one
    }
  }
else
  $_SESSION['vistList']=/* new site URL */
?>
于 2012-09-04T22:16:52.533 回答
1

我会使用 PHP 会话来做到这一点。看看这个例子

将可用页面数组存储在会话变量中。每次获得一个页面时,都会从数组中删除该页面。当数组为空时,您再次从原始源重置它。

您的代码可能如下所示:

session_start();
if (empty($_SESSION["pages"]))
    $_SESSION["pages"] = file("randomlinks.txt");

$nl = count($_SESSION["pages"]);
$np = mt_rand(0, $nl-1);

// get the page, remove it from the array, and shift all higher elements down:
list($url) = array_splice($_SESSION["pages"], $page, 1);

die(header("Location: $url"));
于 2012-09-04T22:42:52.023 回答