dongnong3019 2015-07-21 15:56
浏览 58
已采纳

确保每个用户的点击仅在定义的时间段内发送一次

Hi I have the following code which I am trying to adapt to make sure that when Like is clicked by a user who is logged in, then only one request is sent in a predetermined period of time that can be adjusted i.e Like can be clicked and a request sent only once every 5 minutes. There must be a javascript function I can use but I can't figure it out.

index.php:

<?php
  include 'init.php';
  include 'connect.php';
?>
<!doctype html>
  <html>
    <body>
      <?php       
        $userid = $_SESSION['user_id'];
        echo '<a class="like" href="#" onclick="like_add(', $userid,');">Like</a>';
      ?>
      <script type ="text/javascript" src="jquery-1.11.1.min.js"></script>
      <script type ="text/javascript" src="like.js"></script>      
  </body>
</html>

connect.php:

<?php  
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "DB";
  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }
?>

init.php:

<?php
  session_start();
  $_SESSION['user_id']='1';
  $userid = $_SESSION['user_id'];
  include 'connect.php';
  include 'like.php';
?>

like.js:

function like_add(userid) {
  $.post('like_add.php', {userid:userid}, function(data) {
    if (data == 'success'){
      add_like($userid);
    } else {
      alert(data);
    }
  });
}

like.php:

<?php
  function add_like($userid) {
    include 'connect.php';

    $stmt = $conn->prepare("INSERT INTO clicks (user) VALUES (?)");
    $stmt->bind_param("s", $userid);

    $stmt->execute();
    $stmt = $conn->prepare("SELECT max(id) FROM clicks WHERE user=?");
    $stmt->bind_param("s", $userid);
    $stmt->execute();
    $stmt->bind_result($click);
    $stmt->fetch();
    echo $click;
    $stmt->close();
  }
?

like_add.php

<?php
  include 'init.php';
  if (isset($userid)) {
    $userid = $userid;
    add_like($userid);
  }
?>
  • 写回答

3条回答 默认 最新

  • doupao6698 2015-07-21 16:09
    关注

    If you pass the a tag into like_add like so:

    <?php       
        $userid = $_SESSION['user_id'];
        echo '<a class="like" href="#" onclick="like_add(this, '.$userid.');">Like</a>';
    ?>
    

    You can disable it in the javascript function for a set time period:

    function like_add(a, userid) {
        a.disabled = true;
        setTimeout(function(){
             a.disabled = false;
        ), 5000});//Code will execute in 5 seconds to enable the a tag
    
        $.post('like_add.php', {userid:userid}, function(data) {
            if (data == 'success'){
                add_like($userid);
            }else{
                alert(data);
            }
        });
    }
    

    Is this something alogn the lines of what you were looking for?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析