在现在的好多网站上都出现了每日签到以及签到送礼等互动功能,这边呢,作者就使用最原始的PHP配合MYSQL、AJAX来制作了一个简单的每日签到功能,至于送礼还有其他的小游戏就需要读者们自己去增加和拓展了。
下面先看下文件目录:
database:
顾名思义就是存放数据库SQL语句的文件,里面存放了已经设计好的简单的几张表。
js:
存放JS脚本库。
index.php
用的是.PHP为后缀的,其实下面看到后,读者可能会说,里面没PHP代码,直接弄成.HTML的后缀好了,其实大家可以自己去修改的,作者就不更改了。
index.php code:
<html> <head> <meta http-equiv=Content-Type content="text/html;charset=utf-8"> <title>PHP+AJAX+MYSQL实现签到</title> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript"> //page load get num $(window).ready(function(){ doGetDays(); }); //get sign days function doGetDays(){ $.get("/sign.php?do=getDay",{},function(data){ $("#days").html(data); }); } //do sign function doSign(){ $.get("/sign.php?do=sign",{},function(data){ if(data==1){ alert("sign success!"); window.location.href='/'; }else if(data==2){ alert("already sign today! next tomorrow!"); } }); } </script> </head> <body> <a style="cursor:pointer;" onclick="doSign();">签到<span>连续签到<span id="days"></span>天</span></a> </body> </html>
sign.php 该文件就是具体处理签到的PHP代码了,具体的code如下:
<?php if ($_GET['do']=='getDay'){ $db=new mysqli('localhost','root','123456','sign'); $uid=2; $getDay="SELECT `signdays` FROM `user` WHERE `id`= {$uid}"; $day=$db -> query($getDay); $days =$day -> fetch_array(MYSQLI_ASSOC); echo $days['signdays']; } if ($_GET['do']=='sign'){ $db=new mysqli('localhost','root','123456','sign'); $uid=2; $time=time(); //check sign today ? $todayBegin=strtotime(date('Y-m-d')." 00:00:00"); $todayEnd= strtotime(date('Y-m-d')." 23:59:59"); $checkSignSql="SELECT * FROM `sign` WHERE `uid` = {$uid} AND `dateline` < {$todayEnd} AND `dateline` > {$todayBegin} "; $checkSignToday= $db -> query($checkSignSql); $checkSign = $checkSignToday -> fetch_array(MYSQLI_ASSOC); if (empty($checkSign)){ $sql="SELECT * FROM `sign` WHERE `uid` = {$uid} "; $return = $db -> query($sql) -> fetch_array(MYSQL_ASSOC);//check sign table exist uid record ? if (empty($return)){//no $insertSql="INSERT INTO `sign` (`uid`,`dateline`) VALUES ('{$uid}','{$time}') "; $insert = $db -> query($insertSql); $updateSignSql="UPDATE `user` SET `signdays` =1 WHERE `id` = {$uid} "; $db -> query($updateSignSql); echo 1; }else{ // check is continuous ? reset with login set signdays as 0 !!!!! $yesterdayBegin= strtotime(date("Y-m-d",strtotime("-1 day"))." 00:00:00"); $yesterdayEnd= strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59"); $checkContinuSql="SELECT * FROM `sign` WHERE `uid` = {$uid} AND `dateline` < {$yesterdayEnd} AND `dateline` > {$yesterdayBegin}"; $checkContinuYesterday = $db ->query($checkContinuSql) -> fetch_array(MYSQL_ASSOC); if (!empty($checkContinuYesterday)){ $replaceSql="REPLACE INTO `sign` (`uid`,`dateline`) VALUES ('{$uid}','{$time}') "; $replace=$db -> query($replaceSql); $updateSignSql="UPDATE `user` SET `signdays` = `signdays` + 1 WHERE `id` = {$uid} "; $db -> query($updateSignSql); echo 1; }else{ } } }else{ echo 2;// allready sign. } } ?>
数据库:测试数据库结构及具体内容如下:
-- phpMyAdmin SQL Dump -- version 3.4.8 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Jan 07, 2014 at 01:32 PM -- Server version: 5.5.28 -- PHP Version: 5.2.17p1 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Database: `sign` -- -- -------------------------------------------------------- -- -- Table structure for table `sign` -- CREATE TABLE IF NOT EXISTS `sign` ( `uid` int(11) NOT NULL, `dateline` varchar(10) COLLATE utf8_bin NOT NULL, PRIMARY KEY (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- -- Dumping data for table `sign` -- INSERT INTO `sign` (`uid`, `dateline`) VALUES (1, '1389072071'), (2, '1389072735'); -- -------------------------------------------------------- -- -- Table structure for table `user` -- CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'USERID', `username` varchar(35) COLLATE utf8_bin NOT NULL COMMENT 'username', `signdays` int(11) NOT NULL COMMENT '签到天数', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ; -- -- Dumping data for table `user` -- INSERT INTO `user` (`id`, `username`, `signdays`) VALUES (1, 'ggbound', 4), (2, 'other', 1); /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
到此一个每日签到的功能就基本完成了!
来源:https://www.cnblogs.com/ez1989/p/3508801.html