PHP重定向刷新


PHP redirect refresh

我有一个系统,用户登录并立即定向到一个PHP页面,该页面运行一些查询,它输入到会话变量中。PHP完成执行后,它将用户重定向到主页面。我想知道如何设置它,以便当用户刷新主页时,它将他们重定向到前一个页面,在显示主页之前,计算可以再次运行。我就是不能把这两页合并在一起。有人知道我该怎么做吗?

EDIT: PHP include是最接近我需要的,但问题是,当我使用AJAX提交表单时,第一个文件中的会话变量使用来自数据库的新数据更新。在用户实际刷新页面之前,这些变量需要保持静态。有人知道我如何包含表单,但使其不可见的Jquery?

您可以将一个值存储到$_SESSION,并在每次其他加载时转发到其他页面。

// Start session
session_start();
// If the session variable is set...
if (isset($_SESSION['loadCount']) {
    // Increase the count
    $_SESSION['loadCount']++;
    // If it is even...
    if ($_SESSION['loadCount'] % 2) {
        // Reidrect
        header('Location: /calculate-again.php');
        die();
    }

} else {
    $_SESSION['loadCount'] = 1;
}

在登录表单中,将其提交给page_with_awesome_calculations_and_queries_to_session_variables.php。假设没有在该页中发送输入,在那里进行计算查询和会话,然后向新页发送一个Location头。

header('Location: 'index.php');

这将导致浏览器中的地址栏实际上更改为index.php,并且刷新将使用户停留在那里。

你也应该保护你的page_with_awesome_calculations_and_queries_to_session_variables.php不允许访问,除非正确的$_POST变量设置。

mainpage.php接收一个url参数t,它是epoch时间戳。

calculation.php重定向到mainpage.php时,它将传递epoch时间戳。如果mainpage.php发现epoch时间戳大于5秒,它将重定向回calculation.php并退出。

mainpage.php

if (time() - $_GET['t'] > 5)
{
  header('Location: calculation.php');
  die();
}
else
{
  // proceed normally
}

calculation.php

// perform calculation
header('Location: calculation.php?t=' . time());

添加

<?
require 'calculations.php';
// Mainpage goes here
?>

放到mainPage的顶部。这使得它成为mainPage的扩展,mainPage在加载主页之前运行。