2

I have a bug but everything works great otherwise.

The dialog 'Confirm Form Resubmission' pops up every time the refresh happens. I use a javascript refresh which works without bugs in Chrome, but when I manual refresh I get that dialog. With firefox I need to resend no matter what. The refresh is unavoidable as it is my way to poll a text file that's updated by another computer every minute.

What can I do, to get rid of that dialog?

Here is my php variable

<?php
  $password = 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d';
  session_start();
    if (!isset($_SESSION['loggedIn'])) {
  $_SESSION['loggedIn'] = false;
}

  if (isset($_POST['password'])) {
  if (sha1($_POST['password']) == $password) {
    $_SESSION['loggedIn'] = true;
} else {
    die ('Incorrect password');
  }
} 
  if (!$_SESSION['loggedIn']): 
?>

<html><head><title>Login</title>
<style>
  #formenclosure  {
    width: 300px;
    height:300px;
    margin-top:50px;
    margin-left:auto;
    margin-right:auto;
    color:fff;
       }
</style>
</head>
<body>
<div id="formenclosure">
<fieldset>
<legend>You need to login</legend>
<form method="post">
  Password: <input type="password" name="password"> <br />
  <input type="submit" name="submit" value="Login">
</form>
</fieldset>
</div>
</body>
</html>

<?php
exit();
endif;
?>

here is my refresh code and the require that goes on top of all the documents that you need protected.

<?php
   require('access.php');
?>

<script>
   function refresh() {
     setInterval(function () {
     location.reload(true)
     }, 60000);           // reload page every 60 seconds

  }
 </script>
malanno
  • 95
  • 7
  • 2
    Can't you poll that text file with AJAX? – Jay Blanchard Mar 17 '16 at 18:43
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Mar 17 '16 at 18:44
  • I'm going to look into that Jay, thank you. The problem with polling with ajax is it's all php calls that go to a text file. – malanno Mar 17 '16 at 18:53
  • Then it will go AJAX -> PHP -> file -> PHP -> AJAX. No problem. – Jay Blanchard Mar 17 '16 at 18:55

1 Answers1

0

You can avoid page refresh entirely, and get the exact same result, with a slightly different method:

<script>
    var first_run=true, oldDT='';

    function refresh() {
        setInterval(function () {
            $.ajax({
                type: 'post',
                 url: 'file_check.php',
                success: function(d){
                    if (first_run){
                        oldDT = d;
                        first_run = false;
                    }else{
                        if (d != oldDT) window.location.reload(); //refresh page
                    }
                }
            });
        }, 60000);           // reload page every 60 seconds

    }
</script>

file_check.php

 <?php

    $filename = 'path_to_file/target_file.xml';
    $newDT = date("ymdHis", filemtime($filename));
    echo $newDT;

Simple AJAX explanation/examples

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111