PHP连续签到功能实现方法详解

 更新时间:2019年12月04日 09:05:51   作者:壁虎漫步.  
这篇文章主要介绍了PHP连续签到功能实现方法,结合实例形式详细分析了PHP结合mysql数据库实现连续签到功能相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP连续签到功能实现方法。分享给大家供大家参考,具体如下:

require "./global.php";
  $act = isset($_GET['act']) ? $_GET['act'] : "error";
  // d($act);
  switch($act){
    case "qiandao":
      $uid = intval($_POST['uid']);
      $res = $db -> get('qian_dao','*',array('uid'=>$uid));
      // last_query();
      $time = time();
      //判断今天是否签到
      $todayBegin=strtotime(date('Y-m-d')." 00:00:00"); 
      $todayEnd= strtotime(date('Y-m-d')." 23:59:59"); 
      $isexit = $db -> get('qian_dao','id',array('AND'=>array('uid'=>$uid,'qian_dao_time[>=]'=>$todayBegin,'qian_dao_time[<=]'=>$todayEnd)));
      // last_query();
      if($isexit){
        // echo "今天已签到!";
        Log::writeLog(print_r(array('state'=>'stop','msg'=>'今天已签到!'),true));
        echo json_encode(array('state'=>'stop','msg'=>'今天已签到!'));
        exit;
      }
      if($res){
        //存在签到
        if((time() - $res['qian_dao_time'] > 24*60*60)){ // 判断时间是否大于24小时
          // 让字段归0
          $addInfo = $db -> update('qian_dao',array('qian_dao_num'=>1,'qian_dao_time'=>$time),array('uid'=>$uid));
        }else{
          // 更新签到的天数
          $addInfo = $db -> update('qian_dao',array('qian_dao_num[+]'=>1,'qian_dao_time'=>$time),array('uid'=>$uid));
        }
      }else{
        // echo '您还没有签到过';
        //没有签到过
        $db -> insert('qian_dao',array('uid'=>$uid,'qian_dao_num'=>1,'qian_dao_time'=>$time));
        // echo $db ->last_query();
      }
      // 插入签到记录
      $db -> insert('sign',array(
        'uid'=>$uid,
        'dateline'=>$time,
      ));
      // 获取连续签到的天数
      $info = $db -> get('qian_dao','qian_dao_num',array('uid'=>$uid));
      echo json_encode(array('state'=>'success','msg'=>"您是第".$info."天签到"));
      break;
    default :
      echo json_encode(array("ret"=>0,"msg"=>"操作不存在!"));
      break;
  }

sql:

CREATE TABLE IF NOT EXISTS `qian_dao` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `uid` int(1) NOT NULL COMMENT '用户id',
 `qian_dao_num` int(11) NOT NULL COMMENT '签到次数',
 `qian_dao_time` int(11) NOT NULL COMMENT '签到时间',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `sign` (
 `uid` int(11) NOT NULL,
 `dateline` varchar(10) COLLATE utf8_bin NOT NULL,
 KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

html:

<?php
  include './global.php';
  $uid = 1;
  $todayBegin=strtotime(date('Y-m-d')." 00:00:00"); 
  $todayEnd= strtotime(date('Y-m-d')." 23:59:59"); 
  $isexit = $db -> get('qian_dao','id',array('AND'=>array('uid'=>$uid,'qian_dao_time[>=]'=>$todayBegin,'qian_dao_time[<=]'=>$todayEnd)));
  $flag = '';
  if($isexit){
    $flag = 'current';
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery制作每天或每日打卡签到特效</title>
<meta name="description" content="jquery制作论坛或社交网站的每日或每天打卡签到特效,点击打卡标签显示打卡签到效果。jquery下载" />
</head>
<body>
<style type="text/css">
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;text-decoration:none;}
/*今日签到*/
.singer{border:1px solid #DCDBDB;padding:10px;height:45px;line-height:45px;width:290px;margin:20px auto;}
.ul{border:1px solid #DCDBDB;padding:0 10px 10px 10px;;width:290px;margin:20px auto;}
.li{border:1px solid #DCDBDB;padding-left:10px;height:25px;line-height:25px;width:280px;margin:10px 0 0 0;}
.singer_l_cont, .singer_r_img{float:left;}
.singer_l_cont{width:145px;background:url(images/sing_per.gif) no-repeat left 12px;text-indent:23px;font-size:12px;}
.singer_r_img{display:block;width:114px;height:52px;background:url(images/sing_week.gif) right 2px no-repeat;vertical-align:middle;float:right;*margin-bottom:-10px;}
.singer_r_img:hover{background-position:right -53px;text-decoration:none;}
.singer_r_img span{margin-left:14px;font-size:16px;font-family:'Hiragino Sans GB','Microsoft YaHei',sans-serif !important;font-weight:700;color:#165379;}
.singer_r_img.current{background:url(images/sing_sing.gif) no-repeat 0 2px;}
</style>
  <div class="singer">
    <div class="singer_l_cont">
      <span>每天签到赢取PK币</span>
    </div>
    <div class="singer_r_r">
      <a class="singer_r_img <?php echo $flag;?>" href="#" rel="external nofollow" >    
        <span id="sing_for_number"></span>
      </a>
    </div>
  </div><!--singer end-->
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
/*签到模块日期捕捉:*/
function week(){
  var objDate= new Date();
  var week = objDate.getDay();
  switch(week)
    {
      case 0:
      week="周日";
      break;
      case 1:
      week="周一";
      break;
      case 2:
      week="周二";
      break;
      case 3:
      week="周三";
      break;
      case 4:
      week="周四";
      break;
      case 5:
      week="周五";
      break;
      case 6:
      week="周六";
      break;
    }
  $("#sing_for_number").html( week );
}
$(document).ready(function(){
  week();
  var cache=new Array(); // 缓存变量,当数据被访问过之后放置在缓存中,加快访问速度
  $(".singer_r_img").click(function(){
    // 如果缓存中存在数据,那么直接从缓存中读取;如果不存在数据,那么就从数据库中读取,并把数据存入缓存
    if (typeof(cache['stop'])=='undefined') {
    $.ajax({
      url:"action.php?act=qiandao",
      type:"post",
      dataType:'json',
      data:{
        uid:1,
      },
      async:false,
      success:function(data){
        // alert(data.msg);
        switch(data.state){
          case 'success':
            alert(data.msg);
            break
          case 'stop':
            cache['stop'] = data.msg;
            alert(data.msg);
            break;
        }
        $(".singer_r_img").addClass("current");
      }
    })
    }else{
      alert(cache['stop'])
    }
  })
})
</script>
</body>
</html>
<?php
  // 获取签到记录
  $sign = $db -> select('sign','*',array('uid'=>$uid,'ORDER'=>'dateline DESC'));
?>
<ul class='ul'>
  <?php
  if(empty($sign)){
  ?>
    <li class="li">暂无签到信息</li>
  <?php
  }else{
    foreach($sign as $k=>$v){
  ?>
      <li class="li"><span><?php echo ($k + 1);?></span>&nbsp;<?php echo date('Y-m-d H:i:s',$v['dateline']);?></li>
  <?php
    }
  }
  ?>
</ul>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

  • php数组中删除元素的实现代码

    php数组中删除元素的实现代码

    之前如果要在某个数组中删除一个元素,我是直接用的unset,也不管unset之后会发生什么。但今天看到的东西却让我大吃一惊
    2012-06-06
  • PHP $_FILES函数详解

    PHP $_FILES函数详解

    在PHP中上传一个文件建一个表单要比ASP中灵活得多。具体的看代码。
    2011-03-03
  • PHP中设置时区方法小结

    PHP中设置时区方法小结

    今天发现一段PHP代码中的时间判断语句出了点问题,研究了一下发现问题出在PHP的时区设置上,PHP所取的时间默认是格林威治标准时间,所以和北京时间相差8小时
    2012-06-06
  • php制作基于xml的RSS订阅源功能示例

    php制作基于xml的RSS订阅源功能示例

    这篇文章主要介绍了php制作基于xml的RSS订阅源功能,结合实例形式分析了RSS订阅源文件生成类的定义与使用方法,需要的朋友可以参考下
    2017-02-02
  • PHP图片处理之使用imagecopyresampled函数裁剪图片例子

    PHP图片处理之使用imagecopyresampled函数裁剪图片例子

    这篇文章主要介绍了PHP图片处理之使用imagecopyresampled函数裁剪图片例子,本文例子相对简单,是一篇入门级的学习总结,需要的朋友可以参考下
    2014-11-11
  • PHP+Redis开发的书签案例实战详解

    PHP+Redis开发的书签案例实战详解

    这篇文章主要介绍了PHP+Redis开发的书签案例,结合实例形式详细分析了php结合redis开发书签功能的具体步骤及相关操作技巧,需要的朋友可以参考下
    2019-07-07
  • PHP数组排序函数合集 以及它们之间的联系分析

    PHP数组排序函数合集 以及它们之间的联系分析

    本篇文章是对PHP数组排序函数合集以及它们之间的联系进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP中替换键名的简易方法示例详解

    PHP中替换键名的简易方法示例详解

    默认输出的时候,将数据库字段名作为数组的键名进行输出,但带有键名的数据不能够满足未知情况下的操作,下面为大家介绍个不错的方法可以解决这个问题
    2014-01-01
  • php实现json编码的方法

    php实现json编码的方法

    这篇文章主要介绍了php实现json编码的方法,实例分析了php实现json编码转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • php操作redis常见方法示例【key与value操作】

    php操作redis常见方法示例【key与value操作】

    这篇文章主要介绍了php操作redis常见方法,结合实例形式分析了PHP针对Redis key与value的各种常见操作技巧,需要的朋友可以参考下
    2020-04-04

最新评论