已打开时锁定页面

Lock page when it's already opened

我正在开发一个易于内部使用的 cms。通过这个 cms,用户可以创建和编辑一些产品的页面。我想提醒用户尝试编辑已被其他用户打开的特定产品页面。

我正在使用以下脚本,但我不知道如何轻松提醒第二个用户,例如 "ehi, this page is editing by another user",单击 "ok" 按钮并将他重定向到主页。

我正确地跟踪了所有信息,只是我不知道如何提醒第二个用户并为他锁定页面。

提前致谢!

产品页面

<script src="pages/includes/jsHandler.js"></script>
  <script>
  window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['PATH_INFO']); ?>');};
  window.onbeforeunload = function(){  return end(); };
  window.onclose = function(){ return end(); };
</script>

jsHandler.js

  function start(pageName) {
    var xmlhttp3;
    if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
        } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
}
      xmlhttp3.onreadystatechange=function() {
        if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {
        }
      }
      xmlhttp3.open("GET","pages/includes/trackStart.php?pageName="+pageName,true);
      xmlhttp3.send();
    }

    function end() {
        var xmlhttp3;
        if (window.XMLHttpRequest) {
        xmlhttp3 = new XMLHttpRequest();
        } else {
        xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
    }
      xmlhttp3.onreadystatechange=function() {
        if (xmlhttp3.readyState==4 && xmlhttp3.status==200) {
        }
      }
        xmlhttp3.open("GET","pages/includes/trackEnd.php",true);
        xmlhttp3.send();
    }

trackStart.php

<?php session_start();
date_default_timezone_set("Europe/Rome");
require("dbHandler.php");
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['startTime'] = $_SERVER['REQUEST_TIME'];
$_SESSION['pageName'] = $_GET['pageName'];

if(isset($_SESSION['pageViews']))
{
    $_SESSION['pageViews'] = $_SESSION['pageViews'] + 1;
}
else
{
    $_SESSION['pageViews'] = 0;
}

$ip = $_SESSION['ip'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
$user_location = $details->city . " , " . $details->country;

saveData($_SESSION['ip'],$_SESSION['startTime'],'0',$_SESSION['pageName'],$_SESSION['pageViews'],'0','start',$user_location);
?>

trackEnd.php

<?php session_start();
date_default_timezone_set("Europe/Rome");
require("dbHandler.php");
$_SESSION['endTime'] = $_SERVER['REQUEST_TIME'];
$timeSpent = $_SESSION['endTime'] - $_SESSION['startTime'];
$timeSpent = $timeSpent . " seconds";

saveData($_SESSION['ip'],$_SESSION['startTime'],$_SESSION['endTime'],$_SESSION['pageName'],$_SESSION['pageViews'],$timeSpent,'end');
?>

dbHandler.php

<?php
function saveData($ip,$startTime,$endTime,$pageName,$viewCount,$timeSpent,$action,$location)
{
    $mysqli = new mysqli("localhost", "blabla", "blabla", "blabla");

    if($action == 'start')
    {

        $q = "INSERT INTO track_user(ip_address,start_time,end_time,page_name,view_count,time_spent,user_location) VALUES('$ip','$startTime','$endTime','$pageName','$viewCount','$timeSpent','$location')";
    $mysqli->query($q);

    }
    else
    {
        $q = "UPDATE track_user SET end_time = '$endTime', time_spent = '$timeSpent' WHERE ip_address = '$ip' AND start_time = '$startTime' AND page_name = '$pageName'";
    $mysqli->query($q);
    }
}
?>

已解决!我做了一个 php api select 当前的 pageName。如果 pageName 相等且 end_time = 0,则意味着该页面已打开并且我设置 $risultato = 1。在我的 html 中,我使用此代码提醒用户(Boostrap 模式):

<script>
  $('body').append('<div class="modal fade" id="session-locked" tabindex="-1" role="dialog" aria-hidden="true">'+
                     '<div class="modal-dialog">'+
                         '<div class="modal-content">'+
                             '<div class="modal-header bg-primary">'+
                                 '<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="icons-office-52"></i></button>'+
                                 '<h4 class="modal-title">Scheda occupata</h4>'+
                             '</div>'+
                             '<div class="modal-body">'+
                                 '<p><br />La scheda è occupata da un altro utente.<br />Clicca OK per tornare in Homepage</p>'+
                             '</div>'+
                             '<div class="modal-footer">'+
                                 '<button id="idle-locked-dialog-keepalive" type="button" class="btn btn-primary btn-embossed" data-dismiss="modal">OK</button>'+
                             '</div>'+
                         '</div>'+
                     '</div>'+
                   '</div>');
  var page = '<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['PATH_INFO']); ?>';
  var time = '<?php echo ($_SERVER['REQUEST_TIME']); ?>';
      var jqxhr = $.ajax({url: api_ricerca_lock, type: "GET",dataType: "json", data: {pageName: page, startTime: time}} )
      .done(function(json) {
        if (json.risultato == 1) {
          $('#session-locked').modal('show');
          $('#idle-locked-dialog-keepalive').on('click', function () {
              $('#session-locked').modal('hide');
              window.location = "main.php";
          });
        }
        else {
          window.onload = function(){ return start('<?php echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['PATH_INFO']); ?>');};
          window.onbeforeunload = function(){  return end(); };
          window.onclose = function(){ return end(); };
        }
      })
      .fail(function(response) {
        alert( "error" );
      });
</script>