首页 > 解决方案 > 页面加载时的 PHP 获取 UNIX 时间戳并保存在文本文件中

问题描述

我有几个相同的html页面,其中包含表单和提交表单按钮。发送表单时,我在新页面上重定向,并在发送表单时的文本文件中保存 UNIX 时间戳,这也指示用户何时使用 PHP 进入新页面。问题出在最后一页上,我没有表格,也不知道如何获取时间戳。

PHP

if(isset($_POST['submit'])){
        $t=time();
        $myfile1 = fopen("temp/time.txt", "w");
        fwrite($myfile1, $t.";\n"); 
        //and other not relevant operations
        header("Location: main2.html");       
 }
 if(isset($_POST['submit2'])){
        $t=time();
        $myfile1 = fopen("temp/time.txt", "w");
        fwrite($myfile1, $t.";\n"); 
        //and other not relevant operations
        header("Location: main3.html");       
 }
 if(isset($_POST['submit3'])){
        $t=time();
        $myfile1 = fopen("temp/time.txt", "w");
        fwrite($myfile1, $t.";\n"); 
        //and other not relevant operations
        header("Location: main4.html");       
 }
 if(isset($_POST['submit4'])){
        $t=time();
        $myfile1 = fopen("temp/time.txt", "w");
        fwrite($myfile1, $t.";\n"); 
        //and other not relevant operations
        header("Location: success.html");       
 }

所以,问题是我不能这样做,if(isset($_POST['...'])){因为我在success.html 上没有表格。那么有没有办法获取 UNIX 时间戳,以便知道用户何时进入最后一页或离开前一页?

提前谢谢您,如果问题太简单,我深表歉意。

标签: phphtml

解决方案


我会避免一次又一次地重复相同的代码

//this will happen every time i run this php
$t=time();
$myfile1 = fopen("temp/time.txt", "w");
fwrite($myfile1, $t.";\n"); 
//and other not relevant operations

//this only if isset one of the submit
if(isset($_POST['submit'])){
        header("Location: main2.html");       
 }
 if(isset($_POST['submit2'])){
        header("Location: main3.html");       
 }
 if(isset($_POST['submit3'])){
         header("Location: main4.html");       
 }
 if(isset($_POST['submit4'])){
        header("Location: success.html");       
 }

因此,您将在每次访问此 php 页面时存储日期。然后,如果根据您的条件有必要,您将被重定向


推荐阅读