简体   繁体   English

php阻止页面刷新时重新加载数据

[英]php prevent data reload on page refresh

I have problem SIMILAR to preventing form data reposting, but not quite the same . 我有问题SIMILAR防止表单数据重新发布, 但不完全相同 That question has been answered many times. 这个问题已被多次回答。 The advice given, like header redirects and sessions, didn't work for me. 给出的建议,如标题重定向和会话,对我来说不起作用。 Whether they failed because of my incorrect implementation, lack of experience solving this problem, or both, I don't know. 他们是否因为我的错误实施而失败,缺乏解决这个问题的经验,或两者兼而有之,我不知道。

I have an intranet web app that, when launched, extracts data from a different server, transforms it, and then loads it to a database local to the application. 我有一个Intranet Web应用程序,在启动时,从不同的服务器提取数据,转换它,然后将其加载到应用程序本地的数据库。 What happens is that if the user refreshes the main page, the ETL scripts run again and duplicate the data. 如果用户刷新主页,则会再次运行ETL脚本并复制数据。

How can prevent the ETL scripts from re-running? 如何防止ETL脚本重新运行? For this app, there is no reason to refresh the page. 对于这个应用程序,没有理由刷新页面。 Should I simply prevent refreshes altogether on that page, if that's even possible? 我是否应该在该页面上完全防止刷新,如果可能的话? I'm probably overlooking something simple. 我可能忽略了一些简单的事情。 So, I'm open to suggestions. 所以,我愿意接受建议。

UPDATE: 更新:

I got this test code to work on the index.php page. 我得到了这个测试代码来处理index.php页面。 So, I'm going to build on that to craft a solution. 所以,我将在此基础上制定解决方案。 I'll keep you posted. 我会及时向大家发布。

session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data

I've outlined a very basic example for you of how you could use a session to make sure your scripts are only run once per visit (per user). 我已经为您概述了一个非常基本的示例,说明如何使用会话来确保每次访问(每个用户)只运行一次脚本。 The key point here is the script would still run more than once in the event of more than 1 user using the system. 这里的关键点是,如果超过1个用户使用该系统,脚本仍会运行多次。

To get around this I would simply use your local db to keep track of when the scripts were last run. 为了解决这个问题,我只需使用您的本地数据库来跟踪上次运行脚本的时间。 So you could have a datetime column in your db that would keep track of when the last 'run' was performed. 因此,您可以在数据库中拥有一个日期时间列,以跟踪执行上次“运行”的时间。

  1. So in your PHP code (in replace of checking the $_SESSION) you would query the db to retrieve the datetime value 因此,在PHP代码中(替换检查$ _SESSION),您将查询数据库以检索日期时间值
  2. Once you have the last run date/time you would check if the current date/time is past the allowable 'update time' (for example 24hrs) 一旦有了上次运行日期/时间,您将检查当前日期/时间是否超过允许的“更新时间”(例如24小时)
  3. If the time is past your alloted 'update time' say 25hrs the code would run and you would update the datetime to now() 如果时间已超过您的分配'更新时间',则表示代码将运行25小时,您将日期时间更新为now()
  4. If the time is within your alloted time, nothing would happen, no update of db and your scripts would not run. 如果时间在您的分配时间内,则不会发生任何事情,不会更新db并且您的脚本将无法运行。
  5. Repeat 3 - 4. 重复3 - 4。

This above way would make the number of users irrelevant as all checks will be performed against a central data set (ie your db). 上述方法会使用户数量无关,因为所有检查都将针对中央数据集(即您的数据库)执行。 It would also probably be a bit more robust as it will be independent of browser and user. 它也可能更健壮,因为它将独立于浏览器和用户。

Anyway here is a session (per user) method: 无论如何这里是一个会话(每用户)方法:

<?php
/* 
 * Set PHP sessions to suit:
 * 
 * session.gc_maxlifetime is the number of seconds after which the data 
 * will be seen as garbage and cleaned up (session life) - 
 * 
 * Set to 86400 to in theory make the session last 24 hours - adjust this 
 * number to suit.
 * 
 * If this doesn't work you may have to try and adjust the php session lifetime
 * in either the php.ini file directly or htaccess file.
 * 
 */
ini_set('session.gc_maxlifetime',86400);
/* 
 * not strictly neccessary as this is default setting (but just to make sure)
 * This will keep the cookie alive (the link to your session) until the user
 * closes their browser. 
 * 
 * You could set it to > 86400 if you want that extra bit of certainity that 
 * the session will get renewed @ 24 hrs / the time you wanted to.
 */ 
ini_set('session.cookie_lifetime',0);
// Start PHP session
session_start(); 

// Check if the session is empty / exists (which it will be on first page entry)
if ( empty($_SESSION['hasVisited']) ) {

  /* 
   * Add your ETL script logic here:
   * 
   * This will only run on the first visit to the site
   */   
}

// Now set the session as the scripts have run once
$_SESSION['hasVisited'] = true;

// Continue on with your app...................................................

?>

Let me know if I missed what you were trying to do or you want further explanations on anything? 如果我错过了你想要做的事情,或者你想要对任何事情做进一步的解释,请告诉我?

Look into the POST/REDIRECT/GET pattern . 查看POST / REDIRECT / GET模式 ( Disclaimer, I am the author of that article ) 免责声明,我是那篇文章的作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM