PHP implementation of the check-in function of the method Example analysis de detailed

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the check-in function, combined with examples of PHP based on database and time to determine the operation of the registration function to achieve the relevant skills, the need for friends can refer to the following

In this paper, we describe the method of PHP implementing check-in function. Share to everyone for your reference, as follows:

First of all, I built two tables in the database, one is the user's integration table, one is the check-in status table, which is used to record the user's number of points and first-to-state

In the User Registration status table We have a field, last_sign_time, that is, the last check-in time, each time you can check in to compare this time with the current time if the difference is 0 days, then the registration is today (this check-in is 24 hours can only sign in once, That is, two check-in time is 24 hours or more. If it is equal to one day, you can sign in today, if the 2 days and above indicate the missing sign.

Through the judge of timestamp, update the state table in time, and when can check-in, the User integration table is operated, that is, update the user points.

The specific code is as follows:

<?php$user_id=@$_request[' user_id '];//function: Calculates the difference between two timestamp seconds//$begin _time start timestamp//$end _time end timestamp function Timediff ($begin _time, $end _time)     {if ($begin _time < $end _time) {$starttime = $begin _time;   $endtime = $end _time;     }else{$starttime = $end _time;   $endtime = $begin _time;   }//calculates the number of days $timediff = $endtime-$starttime;   $days = Intval ($timediff/86400);   Count hours $remain = $timediff% 86400;   $hours = intval ($remain/3600);   Calculates the number of minutes $remain = $remain% 3600;   $mins = Intval ($remain/60);   Calculates the number of seconds $secs = $remain% 60;   $res = Array ("Day" and "$days", "hour" + $hours, "min" = = $mins, "SEC" and "$secs"); return $res;} function Upuserscore ($current _total_day) {$user _id=@$_request[' user_id '];//reads the INI file for database configuration information $ary =parse_ini_file ('  Db.ini ');  $db _login_name=base64_decode ($ary [' db_login_name ']);  $db _login_password=base64_decode ($ary [' Db_login_password ']);  $db _host=base64_decode ($ary [' db_host ']); $db _name=base64_decode ($ary [' db_name ']);//LINK Database $scorelink =mysqli_coNnect ($db _host, $db _login_name, $db _login_password); mysqli_select_db ($scorelink, $db _name); Select the database//Check the table if there is a user has the update data is not inserted data $scoreq = "SELECT * from User_score WHERE user_id= $user _id";    SQL INSERT statement mysqli_query ($scorelink, "SET NAMES UTF8"); $rs = Mysqli_query ($scorelink, $scoreq); Gets the DataSet if (! $rs) {exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 ', ' Error ' =>mysqli_error ($scoreli    NK)));       if (Mysqli_num_rows ($rs)) {//the user has data to update their points $total _score= "";      while ($row = Mysqli_fetch_row ($rs)) {$total _score= $row [4]; } $scoreusdateq = "UPDATE user_score SET user_total_score = ' 98 ' WHERE user_id= $user _id;";      Update Mysqli_query ($scorelink, "SET NAMES UTF8"); $ustaters = Mysqli_query ($scorelink, $scoreusdateq);        Get the DataSet if ($ustaters ==1) {exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 ')));      Mysqli_close ($scorelink);//close connection exit (); }else{exit (Json_encode (' status ' = ' SucceSS ", ' Code ' =" 1 ")));      Mysqli_close ($scorelink);//close connection exit ();  }}else{//inserts its integration information without the user data $SCOREINSERTQ = "INSERT into User_score (User_id,user_total_score) VALUES ($user _id,10)";      SQL INSERT statement mysqli_query ($scorelink, "SET NAMES UTF8"); $rs = Mysqli_query ($scorelink, $SCOREINSERTQ); Gets the DataSet if (! $rs) {exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 ', ' Error ' =>mysqli_error ($sco      relink)));      } if (Strpos ($q, "select") = = = False) {exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 ')));  } mysqli_close ($scorelink);//close connection}}//INI file reading database configuration information $ary =parse_ini_file (' Db.ini ');  $db _login_name=base64_decode ($ary [' db_login_name ']);  $db _login_password=base64_decode ($ary [' Db_login_password ']);  $db _host=base64_decode ($ary [' db_host ']); $db _name=base64_decode ($ary [' db_name ']);//Link Database $link =mysqli_connect ($db _host, $db _login_name, $db _login_password ); mysqli_select_db ($link, $db _name); Select a database//check the sign-in table forIf the user has the update data, then insert the data $q = "SELECT * from User_signin WHERE user_id= $user _id";    SQL INSERT statement mysqli_query ($link, "SET NAMES UTF8"); $rs = Mysqli_query ($link, $q); Gets the DataSet if (! $rs) {exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 ', ' Error ' =>mysqli_error ($link)))    );      if (Mysqli_num_rows ($rs)) {//the user has data to update their check-in information $last _time= "";       $total _day= ""; while ($row = Mysqli_fetch_row ($rs)) {$last _time= $row [4]. "      <br/> ";    $total _day= $row [5];      }//According to the last check-in time and the check-in time to make a comparison to determine if there is a missing sign and today is registered $current _total_day=intval ($total _day) +1;      $current _day=time ();    $ary = Timediff ($last _time, $current _day);        if ($ary [day]==0] {//Sign in Exit today (Json_encode (' status ' = ' success ', ' Code ' = ' 5 '));    Mysqli_close ($link);//close connection exit (); }else if ($ary [day]==1) {//no missing $usdateq = "UPDATE user_signin SET last_sign_time= ' $current _day ', total_day= ' $current _t Otal_day ' WHERE user_id= $user _id; "; Update Mysqli_query ($link, "SET NAMES UTF8 "); $ustaters = Mysqli_query ($link, $usdateq);          Gets the dataset if ($ustaters ==1) {upuserscore ($current _total_day);        Exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 '));      Mysqli_close ($link);//close connection exit ();           }else{exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 '));      Mysqli_close ($link);//close connection exit (); }}else{//missed $usdateq = "UPDATE user_signin SET last_sign_time= ' $current _day ', Total_day=1 WHERE user_id= $user _id;" ;    Update Mysqli_query ($link, "SET NAMES UTF8"); $ustaters = Mysqli_query ($link, $usdateq);        Gets the dataset if ($ustaters ==1) {upuserscore ($current _total_day);      Exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 '));    Mysqli_close ($link);//close connection exit ();       }else{exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 '));    Mysqli_close ($link);//close connection exit ();    }}}else{//Insert $current _day=time () without the user record; $insertq= "INSERT into User_signin (user_id,last_sign_time,total_day) VALUES ($user _id, ' $current _day ', 1)";    SQL INSERT statement mysqli_query ($link, "SET NAMES UTF8"); $insertrs = Mysqli_query ($link, $INSERTQ);      Get data Set if (Strpos ($insertrs, "select") = = = False) {//Access data successfully upuserscore (' 1 ');      Exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 ')); }} mysqli_close ($link);//close Connection?>

Articles you may be interested in:

Solve the problem of memory exhaustion due to excessive php-fpm process in Linux

PHP implementation of the check-in function of the method Example analysis

Introduction to PHP-FPM parameter configuration and explanation of parameter optimization under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.