PHP功能将链接重定向到我的网站上的随机页面只能工作一次。我该如何使其每次工作?

发布于 2025-02-02 14:32:37 字数 1005 浏览 2 评论 0原文

首先,这不是我的代码,我在将其进入此阶段方面得到了很多帮助。我的问题是:随机重定向可完美地工作,如果您在网站上单击一段时间或清除浏览历史记录,它将再次起作用。因此,仿佛RNG会生成一个随机页面一次,但是随后记住该页面并在其后每次链接到它。

如下所示,链接转到一个称为“随机页面”的虚拟页面,然后将其重定向到网站上的随机页面(不包括第5行上列出的许多页面)。问题似乎是RNG的种子没有重置。我一直与我的软件工程师邻居合作,我们尝试了各种事情,但没有任何效果。一方面,我们尝试将第7行上的array_rand更改为MT_RAND,但这引起了“重定向太多”错误。

原始代码是:

add_action('template_redirect', 'wpsf_random_page_redirect');
function wpsf_random_page_redirect()
{
    if (is_page('random-page')) {
        $excluded_pages = array(125, 126, 508, 510, 723, 987, 995, 1079, 1109, 1121, 1135, 1146, 1159, 1177, 1188, 1418, 1433);
        $pages = get_pages(array('exclude' => $excluded_pages));
        $random_page = $pages[array_rand($pages)];
        $page_url = get_page_link($random_page);
        wp_redirect($page_url);
        exit;
    }
}

我们尝试添加第二个虚拟页面,添加srand();进入代码或各个位置,例如虚拟页面元描述等等。我们认为,我们最接近解决方案的尝试是将array_rand更改为mt_rand,并遇到了太多的重定向错误。

我全部环顾了网络,无法弄清楚(部分是因为所有这些主要是对我来说是胡言乱语),但是我的邻居似乎也不知道,他知道他在做什么。任何想法都将不胜感激,谢谢!

First of all, this isn't my code and I've received a lot of help in getting it to this stage. My problem is this: the random redirect works perfectly once, and if you click around the site for a while or clear your browsing history, it will work again. So it's as if the rng generates a random page once but then remembers that page and links back to it each time after.

As you'll see below, a link goes to a dummy page called "random page" which then redirects to a random page on the site (excluding a number of pages listed on line 5). The issue seems to be that the seed for the rng isn't resetting. I've been working with my software engineer neighbor and we've tried all kinds of things but nothing has worked. For one thing, we tried changing array_rand on line 7 to mt_rand, but this caused a "too many redirects" error.

The original code is this:

add_action('template_redirect', 'wpsf_random_page_redirect');
function wpsf_random_page_redirect()
{
    if (is_page('random-page')) {
        $excluded_pages = array(125, 126, 508, 510, 723, 987, 995, 1079, 1109, 1121, 1135, 1146, 1159, 1177, 1188, 1418, 1433);
        $pages = get_pages(array('exclude' => $excluded_pages));
        $random_page = $pages[array_rand($pages)];
        $page_url = get_page_link($random_page);
        wp_redirect($page_url);
        exit;
    }
}

We've tried adding a second dummy page, adding srand(); into the code or various locations such as the dummy page meta description, and much more. Our closest attempt to a solution, we think, was changing array_rand to mt_rand and getting the too many redirects error.

I've looked all around the web and couldn't figure it out (partly because all this is mostly gibberish to me), but neither could my neighbor it seems, and he knows what he's doing. Any ideas would be much appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2025-02-09 14:32:37

这可能是一个缓存的问题,所以尝试添加nocache_headers();喜欢

add_action('template_redirect', 'wpsf_random_page_redirect');
function wpsf_random_page_redirect()
{
    if (is_page('random-page')) {
        $excluded_pages = array(125, 126, 508, 510, 723, 987, 995, 1079, 1109, 1121, 1135, 1146, 1159, 1177, 1188, 1418, 1433);
        $pages = get_pages(array('exclude' => $excluded_pages));
        $random_page = $pages[array_rand($pages)];
        $page_url = get_page_link($random_page);
        nocache_headers();
        wp_redirect($page_url);
        exit;
    }
}

It might be a caching issue so try adding nocache_headers(); like so

add_action('template_redirect', 'wpsf_random_page_redirect');
function wpsf_random_page_redirect()
{
    if (is_page('random-page')) {
        $excluded_pages = array(125, 126, 508, 510, 723, 987, 995, 1079, 1109, 1121, 1135, 1146, 1159, 1177, 1188, 1418, 1433);
        $pages = get_pages(array('exclude' => $excluded_pages));
        $random_page = $pages[array_rand($pages)];
        $page_url = get_page_link($random_page);
        nocache_headers();
        wp_redirect($page_url);
        exit;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文