锁定页面已经打开

时间:2016-07-04 16:36:28

标签: javascript php locking handler tracking

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

我使用以下脚本,但我不知道如何轻松提醒第二个用户,例如" ehi,此页面正由其他用户编辑",点击" 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);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

解决!我做了一个php api,选择当前的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>