search
HomeBackend DevelopmentPHP TutorialPHP lottery program code for time periods

  1. require_once (APP_ROOT . "./class/class_lib.php");
  2. require_once libfile ( 'class/StoreProcs' );
  3. pc_base::load_app_class('table_sub','',0 );
  4. /**
  5. * Big carousel
  6. * @author Administrator
  7. *
  8. */
  9. class lib_dzp_lottery extends app_lib {
  10. /**
  11. * Prize List
  12. */
  13. public $lottery = 'activity_lyq_lottery';
  14. /**
  15. * Prize Log Sheet
  16. */
  17. public $lottery_log = ' activity_lyq_lottery_log';
  18. /**
  19. * n prizes
  20. */
  21. private $level = array();
  22. /**
  23. * Prize information
  24. */
  25. private $lotteryArray = array();
  26. /***
  27. * Random number ending digits
  28. */
  29. private $max = 30000;
  30. /**
  31. * Category true: Default false: Palm Su
  32. */
  33. private $type=true;
  34. /**
  35. * Winning professional account
  36. */
  37. private $tel_zy=false;
  38. /**
  39. * Read configuration
  40. */
  41. public function load_setting () {
  42. $data=getcache('lyqdzp_setting');
  43. define('TIME', time()); //Current time
  44. define('L_START', strtotime($data['l_start'])); / /Registration start time
  45. define('L_END', strtotime($data['l_end'])); //Registration end time, -1 means yesterday 23:59:59
  46. define('L_DAY_START',$data['l_day_start ']); //Start time of each day
  47. define('L_DAY_END', $data['l_day_end']); //End time of each day
  48. define('L_DAY_CISHU', $data['l_day_cishu']); //Get every day Number of prizes
  49. define('L_CISHU', $data['l_cishu']);//Total number of prizes obtained
  50. define('L_MIN', 20);//Minimum LIFB coins
  51. define('L_LFB', $data[ 'l_lfb']);//Reduce Lifb coins
  52. define('L_ZY', $data['l_zy']); //Professional accounts
  53. define('L_QUANSHU', $data['l_quanshu']);//Circle Count, the number of rounds the big wheel starts to roll
  54. define('L_KONGJIANG', intval($data['l_kongjiang'])); //Probability of empty prize
  55. $host='www.****.com'; //Existing network
  56. if($_SERVER['HTTP_HOST']=='221.****0'){
  57. $host='221.*****'; //Test machine
  58. }elseif($_SERVER['HTTP_HOST ']=='125.com'){
  59. $host='125life.com';//Local
  60. }elseif(strpos($_SERVER['HTTP_HOST'],'192.168.')==0){
  61. $ host=$_SERVER['HTTP_HOST'].'/125e.com';//Local
  62. }
  63. define('L_SITE', 'http://'.$host.'/'); //Add / at the end of the domain name
  64. define('L_URL', L_SITE.'index.php');//Access address
  65. //Can only be accessed through flash address
  66. $s=L_SITE.'flash/dzp/i.swf';
  67. if(preg_replace ('/swf.*$/','swf',$_SERVER['HTTP_REFERER'])!=$s&&$this->type){
  68. exit('Dude, you have got enough, please pass the formal Way to win!');
  69. }
  70. $this->load();
  71. }
  72. /**
  73. * Blacklist
  74. */
  75. private $heimingdan = array();
  76. /**
  77. * Read prize configuration
  78. */
  79. public function load() {
  80. $this->setlottery ();
  81. $this->heimingdan = getcache ( 'tel', 'blanklist' );
  82. }
  83. private function setlottery() {
  84. $time=strtotime(date ('Y-m-d',TIME));
  85. $sql = "select u.*";
  86. $sql .= ", (select COUNT(l.lotteryid) from " . DB::table ( $this-> lottery_log ) . " AS l where u.lotteryid=l.lotteryid and l.iswinning='1')as num ";
  87. $sql .= " from " . DB::table ( $this->lottery ) . " AS u ";
  88. $sql .= " where 1 ";
  89. $sql .= " and u.isdisable='0' ";
  90. $sql .= " and u.time_start$ sql .= " and u.time_end>='$time' ";
  91. $sql .= " ORDER BY u.listorder ";
  92. $d = DB::fetch_all ( $sql );
  93. $sum = 0;
  94. foreach ( $d as $r ) {
  95. $r ['sum_over'] = intval ( $r ['sum'] ) - intval ( $r ['num'] );
  96. if ($r ['sum'] > ; 0) {
  97. $sum += $r ['sum'];
  98. }
  99. $r['msg']=str_replace('{name}', $r['name'],$r['msg' ]);
  100. $this->lotteryArray [$r ['lotteryid']] = $r;
  101. }
  102. if($sum$m = 1;
  103. $probability = 0;
  104. $newsum=0;
  105. //80% empty prize + 20% prize
  106. $this->max = $sum + $sum * (L_KONGJIANG/100) / ((100-L_KONGJIANG)/100);
  107. foreach ( $this->lotteryArray as $r ) {
  108. if ($r ['sum'] > 0) {
  109. $newsum=$r ['sum'];
  110. if($r['probability']> 0){
  111. $probability=0;
  112. $probability=ceil($r ['sum']*100/$r['probability']);
  113. if($probability>0){
  114. $newsum=$probability;
  115. }
  116. }
  117. $this->level [$r ['lotteryid']] ['start'] = $m;
  118. $this->level [$r ['lotteryid']] ['end'] = $m + $newsum;
  119. $m = $m + $r ['sum'];
  120. }
  121. }
  122. }
  123. /**
  124. * Check whether the user has won the prize 0 Thank you for participating, others return prize data
  125. * @param array $user array(uid,tel,ip)
  126. */
  127. public function check($user) {
  128. if (intval ( $user ['uid'] ) //Not logged in
  129. return 0;
  130. }
  131. if(intval(TIME)//You are here early, the event is still going on No start!
  132. return -21;
  133. }
  134. if(intval(TIME)>intval(L_END)){
  135. //The event time has passed, I am very sorry, thank you!
  136. return -22;
  137. }
  138. if ($this->get_user_lfb ( $user ['uid'] ) //$this->credit ( $user ['uid'], -11 , $logid );
  139. return -11; //Your LIF balance is insufficient
  140. }
  141. $data = array(
  142. 'uid' => $user ['uid'],
  143. 'tel' => $user ['tel'],
  144. 'ip' => $user ['ip'],
  145. 'addtime' => time ()
  146. );
  147. $logid = DB::insert ( $this-> lottery_log, $data, true );
  148. //Check the number of prizes the user has won every day
  149. if ($this->get_lottery ( $user ['uid'], strtotime ( date ( 'Y-m-d', TIME ) ), strtotime ( "+1day", strtotime ( date ( 'Y-m-d', TIME ) ) ), true ) >= L_DAY_CISHU) {
  150. $this->credit ( $user ['uid'], -12 , $logid );
  151. //Unfortunately, you didn’t draw anything this time!
  152. return -12;
  153. }
  154. //Check the number of prizes won by the user
  155. if ($this->get_lottery ( $user ['uid'], 0, 0, true ) > L_CISHU&&L_CISHU>0) {
  156. $ this->credit ( $user ['uid'], -12, $logid );
  157. //Unfortunately, you didn’t draw anything this time!
  158. return -15;
  159. }
  160. //Detect blacklist, set probability
  161. if ($this->checkhei ( $user ['tel'] ) > 0) {
  162. $this->credit ( $user [ 'uid'], -13, $logid );
  163. //Unfortunately, you didn't draw anything this time!
  164. return -13;
  165. }
  166. $lotteryid = $this->choujiang();
  167. $this->credit ( $user ['uid'], $lotteryidhao);
  168. if ($lotteryid > 0) {
  169. return $this->lotteryArray [$lotteryid];
  170. } else {
  171. return -4;
  172. }
  173. //-1 prize Already distributed
  174. //-2 Prizes for this period have been played
  175. //-3 Prizes not distributed
  176. //-4 There is a problem with the prizes
  177. //-11 Your Laifu coin balance is insufficient
  178. //-12 Unfortunately, you Nothing was drawn this time!
  179. //-In the blacklist of 13, unfortunately, you didn’t draw anything this time!
  180. //-14 Winning professional accounts are prohibited from talking about high prizes.
  181. //-15 has exceeded the total number of prizes in yizhong.
  182. //-16 The mobile number sent by Zhangsu is wrong.
  183. //-21 You are early and the event has not started yet!
  184. //-22 has passed the event time, I am very sorry, thank you!
  185. //-23Sorry, it is not the event time now, the event time is every day
  186. }
  187. /**
  188. * Show
  189. * @param unknown_type $data
  190. */
  191. public function show($data){
  192. $ok=0;//3 Winning
  193. $zz= 0;//Roulette degree
  194. $result='';//Prize name
  195. $lotteryid=0;//Prize ID
  196. if(is_array($data)){
  197. $ok=3;
  198. $result=$data[ 'msg'];
  199. $zz=$data['angle']+L_QUANSHU*360;
  200. $lotteryid=$data['lotteryid'];
  201. }else{
  202. $m='';
  203. switch($data) {
  204. case 0:{
  205. $m= 'Please log in first! ';
  206. }break;
  207. case -11:{
  208. $m= 'Your Raffles balance is insufficient! ';
  209. }break;
  210. case -12:{
  211. $m= 'Unfortunately, you didn't draw anything this time! ';
  212. }break;
  213. case -21:{
  214. $m= 'You are early and the event has not started yet! ';
  215. }break;
  216. case -22:{
  217. $m= 'The event time has passed, I am very sorry, thank you! ';
  218. }break;
  219. case -23:{
  220. $m= 'Sorry, it is not the event time now, the event time is every day'.L_DAY_START.':00-'.L_DAY_END.':00';
  221. }break;
  222. default:{
  223. $m= 'Unfortunately, you didn't draw anything this time!';
  224. }break;
  225. }
  226. $result=$m;
  227. $ok=3;//3中奖
  228. $zz=360+L_QUANSHU*360;//再接再厉
  229. }
  230. header("Expires:Mon,26Jul199705:00:00GMT");
  231. header("Cache-Control:no-cache,must-revalidate");
  232. header("Pragma:no-cache");
  233. header("Content-type:text/html;charset=utf-8");
  234. if(!$this->type){
  235. echo json_encode(array('return'=>$lotteryid,
  236. 'msg'=>$result));
  237. }else{
  238. echo ''."n";
  239. echo ''."n";
  240. echo ''."n";
  241. echo '
  242. '."n";
  243. }
  244. exit;
  245. }
  246. /**
  247. * Lucky draw number
  248. */
  249. private $hao=0;
  250. /**
  251. *The draw begins
  252. */
  253. private function choujiang() {
  254. $j = 0;
  255. $lotteryid = 0;
  256. $j = mt_rand ( 1, $this->max );
  257. $this->hao=$j;
  258. foreach ( $this->level as $k => $r ) {
  259. if ($r ['start'] $j) {
  260. $lotteryid = $k;
  261. break;
  262. }
  263. }
  264. $d = array(
  265. 1 => array(
  266. 'time' => strtotime ( date ( 'Y-m-d 00:00:00', TIME ) ),
  267. 'time_end' => strtotime ( date ( 'Y-m-d 07:59:59', TIME ) )
  268. )
  269. );
  270. for ($i=2;$i$d[$i]=array(
  271. 'time' => strtotime ( date ( 'Y-m-d '.($i+6).':00:00', TIME ) ),
  272. 'time_end' => strtotime ( date ( 'Y-m-d '.($i+6).':59:59', TIME ) )
  273. );
  274. }
  275. if($lotteryid>0){
  276. $l=$this->lotteryArray [$lotteryid];
  277. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  278. $sql .= " where lotteryid='$lotteryid' ";
  279. $s= intval ( DB::result_first ( $sql ) );
  280. $sum_over=$l['sum']-$s;
  281. if($sum_over>0){
  282. //本时段 奖品数
  283. $sum=0;
  284. foreach ($d as $k=>$r){
  285. if(TIME>=$r['time']&&TIMEif($l['time_'.$k]>0){//个数限制
  286. $sum=$this->get_lottery_id(0,$r['time'],$r['time_end']+1,$lotteryid);
  287. if($sum>=$l['time_'.$k]){
  288. //本时段奖品已玩
  289. $lotteryid=-2;
  290. break;
  291. }
  292. }elseif ($l['time_'.$k]==0){
  293. //不发奖品
  294. $lotteryid=-3;
  295. break;
  296. }else{
  297. //-1 不限制
  298. }
  299. }
  300. }
  301. if($this->tel_zy && $lotteryid>0&&$lotteryid$lotteryid=-14;
  302. }
  303. }else{
  304. //奖品已发完
  305. $lotteryid=-1;
  306. }
  307. }else{
  308. //奖品有问题
  309. $lotteryid=-4;
  310. }
  311. return $lotteryid;
  312. }
  313. /**
  314. * Detect blacklist and ordinary users, probability
  315. * @param string $tel
  316. * @return greater than 1 means that drawing is prohibited this time
  317. */
  318. private function checkhei($tel) {
  319. $n = mt_rand ( 1, 100 );
  320. if (in_array ( $tel, $this->heimingdan ['tel'] ) && L_ZY > 0) {
  321. $this->tel_zy=true;
  322. if ($n > L_ZY) {
  323. return 1;
  324. }
  325. }else{
  326. //号码检测
  327. if (!self::check_tel($tel)) {
  328. return 1;
  329. }
  330. }
  331. return 0;
  332. }
  333. /**
  334. * Total number of prizes
  335. * @param intval $uid
  336. * @param intval $start
  337. * @param intval $end
  338. * @param bool $bool true Number of prizes obtained false No statistics
  339. * @return number
  340. */
  341. private function get_lottery($uid = 0, $start = 0, $end = 0, $bool = false) {
  342. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  343. $sql .= " where 1 ";
  344. if (intval ( $start ) > 0) {
  345. $sql .= " and addtime>='$start' ";
  346. }
  347. if (intval ( $end ) > 0) {
  348. $sql .= " and addtime}
  349. if ($bool) {
  350. $sql .= " and lotteryid>0 ";
  351. }
  352. if (intval ( $uid ) > 0) {
  353. $sql .= " and uid='$uid' ";
  354. }
  355. return intval ( DB::result_first ( $sql ) );
  356. }
  357. /**
  358. * Total number of prizes
  359. * @param intval $uid
  360. * @param intval $start
  361. * @param intval $end
  362. * @param intval $bool prize id
  363. * @return number
  364. */
  365. private function get_lottery_id($uid = 0, $start = 0, $end = 0, $lotteryid = 0) {
  366. $sql = "select count(*) m from " . DB::table ( $this->lottery_log );
  367. $sql .= " where 1 ";
  368. if (intval ( $start ) > 0) {
  369. $sql .= " and addtime>='$start' ";
  370. }
  371. if (intval ( $end ) > 0) {
  372. $sql .= " and addtime}
  373. if ($lotteryid>=0) {
  374. $sql .= " and lotteryid='$lotteryid' ";
  375. }
  376. if (intval ( $uid ) > 0) {
  377. $sql .= " and uid='$uid' ";
  378. }
  379. return intval ( DB::result_first ( $sql ) );
  380. }
  381. /**
  382. * Get the user’s Rafflecoins
  383. * @param intval $uid
  384. * @return number
  385. */
  386. public function get_user_lfb($uid) {
  387. $sql = "select extcredits2 from sz_common_member_count ";
  388. $sql .= " where 1 ";
  389. $sql .= " and uid='$uid' ";
  390. return intval ( DB::result_first ( $sql ) );
  391. }
  392. /**
  393. * Record logs and update user LIF coins
  394. * @param intval $uid
  395. * @param intval $r return value
  396. * @param intval $logid
  397. * @param intval $lotteryid
  398. */
  399. private function credit($uid, $result, $logid = 0, $lotteryid = 0,$hao=0) {
  400. $data = array();
  401. if ($lotteryid > 0) {
  402. $data ['lotteryid'] = $lotteryid;
  403. $data ['iswinning'] = 1;
  404. }
  405. $data ['result'] = $result;
  406. $data ['hao'] = $hao;
  407. $data ['HTTP_REFERER'] = $_SERVER['HTTP_REFERER'];
  408. DB::Update ( $this->lottery_log, $data, " logid='$logid' " );
  409. if (L_LFB > 0) {
  410. $sql = 'UPDATE sz_common_member_count SET extcredits2=extcredits2-' . L_LFB . ' WHERE uid=' . $uid;
  411. DB::query ( $sql );
  412. $this->credit_log ( $uid, 'reduce', L_LFB, 1, '积分清零活动' );
  413. }
  414. }
  415. /**
  416. *Mobile number check
  417. */
  418. private function check_tel($tel){
  419. $different_network=table_sub::get_different_network();
  420. if (!preg_match("/$different_network/",$tel)) {
  421. return false;
  422. }
  423. return true;
  424. }
  425. /**
  426. * Stored procedure link
  427. */
  428. private $StoreProcs;
  429. /**
  430. * Laifu Coin log stored procedure
  431. * @param intval $uid
  432. * @param string $op add to increase reduce consumption
  433. * @param intval $creditvalue Laifu Coin
  434. * @param intval $credittype 1 Laifu Coin 2 Laifu Coin Click
  435. * @param string $oreason description
  436. */
  437. private function credit_log($uid, $op, $creditvalue, $credittype, $oreason) {
  438. if (! $this->StoreProcs) {
  439. $this->StoreProcs = new StoreProcs ();
  440. }
  441. //调用存储过程
  442. $this->StoreProcs->query ( "call app_credit_log($uid,'$op','$credittype',1,$creditvalue,'$oreason')" );
  443. }
  444. }
复制代码

三,调用:

  1. $dzp=new lib_dzp_lottery();
  2. $u=array('uid'=>$_G ['uid'],'tel'=>$_G ['tel'],'ip'=> $_G ['clientip']);
  3. $dzp->load_setting();
  4. $dzp->show($dzp->check($u));
  5. ?>
复制代码


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor