转到随机页面 PHP [英] Going to a Random page PHP

查看:41
本文介绍了转到随机页面 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向 http://example.com/random.php 的链接,下面是 random.php 的代码.

I have a link that goes to http://example.com/random.php, code for random.php below.

<?php
srand ((double) microtime( )*1000000);
$random_number = rand(1,100);
header( "Location: http://example.com/test?page=$random_number" ) ;
?>

基本上我想要它做的是链接到一个随机页面.它最初有效,但在第一次点击后,它每次都会链接回同一个假定的随机页面.知道如何解决这个问题吗?或者可能是完全解决问题的更好方法?

Basically what I want it to do is link to a random page. It works initially, but after the first click it keeps linking back to the same supposedly random page every single time. Any idea how to fix this? or maybe a better way to approach the problem entirely?

推荐答案

我猜 Web 浏览器会缓存 Location 重定向.尝试在页面顶部添加一些缓存破坏"标题

My guess would the web browser is caching the Location redirect. Try adding some "cache busting" headers to the top of the page

<?php
    //from http://php.net/header
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

    srand ((double) microtime( )*1000000);
    $random_number = rand(1,100);
    header( "Location: http://example.com/test?page=$random_number" ) ;
?>

免责声明:如果您将其用于邪恶目的,那么您将受到极客诅咒,您将在短时间内被食人魔吃掉.

DISCLAIMER: If you're using this for nefarious purposes, a geek curse is hereby placed on you, and you will be eaten by a grue in short order.

这篇关于转到随机页面 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆