1. 程式人生 > >php實現每日簽到功能

php實現每日簽到功能

每日簽到的功能,供大家參考,具體內容如下

首次簽到獲得1個積分,第二次簽到獲得2個積分,第三次簽到獲得3個積分,以此類推但是簽到必須每天連續積分才可以遞增,如果有中斷再次簽到時獲得積分仍然從1開始遞增;
user: id,username,count,point,sign_time

sign.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>sign</title>
</head>
<body>
<center>
<input type="text" name="username">
<button>簽到</button><span id='span'></span>
<div>
  <table id="box" border="1"></table>
</div>
</center>
</body>
</html>
<script src="jquery.1.12.min.js"></script>
<script>
  $(function(){
    $('button').click(function(){
      var username=$(':text').val();
      $.ajax({
        type:'post',
        url:'admin.php',
        data:{username:username},
        dataType:'json',
        success:function(res){
          if(res.success==1){
            $('#span').html('簽到成功');
            var str='<tr><td>使用者名稱</td><td>連續簽到天數</td><td>總積分</td></tr>';
            str+='<tr><td>'+res.msg.username+'</td><td>'+res.msg.count+'</td><td>'+res.msg.point+'</td></tr>';
            $('#box').html(str);
          }
        }
      })
    });
  })
</script>

 admin.php

<?php 
  header('content-type:text/html;charset=utf-8');
  $pdo=new PDO('mysql:host=localhost;dbname=databasename;','root','root');
  $pdo->exec('set names utf8');
  $username=$_POST['username'];
  $sqlQuery="select * from user where username='$username'";
  $row=$pdo->query($sqlQuery
)->fetch(PDO::FETCH_ASSOC); if($row){ $sign_time=$row['sign_time']; $sign_time=strtotime($sign_time); $int=date('Y-m-d'); $int=strtotime($int);//5 $ints=$int+86400; //6 $int_s=$int-86400; //4 //當天已簽到 if($int<$sign_time&&$sign_time<$ints){ // echo '您已簽到'; }
//昨天未簽到,積分,天數在簽到修改為1 if($sign_time<$int_s){ $count=1; $point=1; $sign_time=date('Y-m-d H:s:i'); $sqlRow="update user set count='$count',point='$point',sign_time='$sign_time' where username='$username'"; $res=$pdo->exec($sqlRow); // echo '簽到成功修改為1'; } //請簽到 if($int_s<$sign_time&&$sign_time<$int){ $count=$row['count']+1; $point=$row['point']+1; $sign_time=date('Y-m-d H:s:i'); $sqlupdate="update user set count='$count',point='$point',sign_time='$sign_time' where username='$username'"; $res=$pdo->exec($sqlupdate); // echo '簽到成功+1'; } }else{ $count=1; $point=1; $sign_time=date('Y-m-d H:s:i'); $sqlAdd="insert into user values (null,'$username','$count','$point','$sign_time')"; $res=$pdo->exec($sqlAdd); // echo '恭喜你簽到成功----1'; } //////////////////////響應 $sqlEnd="select * from user where username='$username'"; $info=$pdo->query($sqlEnd)->fetch(PDO::FETCH_ASSOC); echo json_encode(array('success'=>1,'msg'=>$info));die; ?>