Php implements the sign-in function, and php implements sign-in.

Source: Internet
Author: User

Php implements the sign-in function, and php implements sign-in.

First, I created two tables in the database. One is the user's integral table, and the other is the sign-in status table, which is used to record the number of user points and the first-come status.

 

In the user sign-in status table, we have a field, last_sign_time, that is, the last sign-in time. Each time we sign in, we can compare this time with the current time. If the difference is 0 days, it indicates that you have signed in today (this sign-in can only be performed once within 24 hours, that is, the two sign-In times must differ by more than 24 hours ). if it is equal to one day, you can sign in today. If it is 2 days or more, the signature is missing.

Based on the timestamp, the status table is updated in a timely manner, and user points are updated when you can sign in.

 

<? Php $ user_id = @ $ _ REQUEST ['user _ id']; // function: calculate the difference between two timestamps by day, hour, and second // $ 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 ;} // computing days $ timediff = $ endtime-$ starttime; $ days = intval ($ timediff/86400); // computing hours $ remain = $ timediff % 86400; $ hours = intval ($ remain/36 00); // calculate the number of minutes $ remain = $ remain % 3600; $ mins = intval ($ remain/60); // calculate the number of seconds $ secs = $ remain % 60; $ res = array ("day" => $ days, "hour" => $ hours, "min" => $ mins, "sec" => $ secs ); return $ res;} function upuserscore ($ current_total_day) {$ user_id =@ _ REQUEST ['user _ id']; // The INI file that reads the 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 _ lo Gin_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 database // check whether the user exists in the integral table. If no data is updated, insert the 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); // obtain the dataset if (! $ Rs) {exit (json_encode (array ('status' => "success", 'code' => "1 ", 'error' => mysqli_error ($ scorelink);} if (mysqli_num_rows ($ rs )) {// if the user has data, the points are updated. $ 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, $ score Usdateq); // obtain the dataset if ($ ustaters = 1) {exit (json_encode (array ('status' => "success ", 'code' => "100"); mysqli_close ($ scorelink); // close the connection exit ();} else {exit (json_encode (array ('status' => "success", 'code' => "1"); mysqli_close ($ scorelink ); // close the connection exit () ;}} else {// if this user data does not exist, insert the Point Information $ scoreinsertq = "insert into user_score (user_id, user_total_score) values ($ user_id, 10) "; // SQL insert statement mysqli_query ($ scorelink," SET NA MES utf8 "); $ rs = mysqli_query ($ scorelink, $ scoreinsertq); // obtain the dataset if (! $ Rs) {exit (json_encode (array ('status' => "success", 'code' => "1 ", 'error' => mysqli_error ($ scorelink);} if (strpos ($ q, "SELECT") = false) {exit (json_encode (array ('status' => "success", 'code' => "100");} mysqli_close ($ scorelink ); // close the connection} // The INI file that reads the 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_h Ost = 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 database // check whether the user exists in the sign-in table. If no data is updated, 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); // obtain the dataset if (! $ Rs) {exit (json_encode (array ('status' => "success", 'code' => "1 ", 'error' => mysqli_error ($ link);} if (mysqli_num_rows ($ rs )) {// if the user has data, update his/her sign-in information $ last_time = ""; $ total_day = ""; while ($ row = mysqli_fetch_row ($ rs )) {$ last_time = $ row [4]. "<br/>"; $ total_day = $ row [5];} // compare the last sign-in time with the current sign-in time to determine whether there is a missing sign or whether the current day has been signed in $ current_total_day = intval ($ total_day) + 1; $ current_day = time (); $ ary = timediff ($ last_time, $ current_day); if ($ Ary [day] = 0) {// exit (json_encode (array ('status' => "success ", 'code' => "5"); mysqli_close ($ link); // close the connection exit ();} else if ($ ary [day] = 1) {// no signature missing $ usdateq = "UPDATE user_signin SET last_sign_time = '$ current_day', total_day = '$ current_total_day' WHERE user_id = $ user_id ;"; // update mysqli_query ($ link, "set names utf8"); $ ustaters = mysqli_query ($ link, $ usdateq); // obtain the dataset if ($ ustaters = 1) {upuserscore ($ current _ Total_day); // exit (json_encode (array ('status' => "success", 'code' => "100"); mysqli_close ($ link ); // close the connection exit ();} else {exit (json_encode (array ('status' => "success", 'code' => "1 "))); mysqli_close ($ link); // close the connection exit () ;}} else {// missed sign $ 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 ($ l Ink, $ usdateq); // obtain the dataset if ($ ustaters = 1) {upuserscore ($ current_total_day ); // exit (json_encode (array ('status' => "success", 'code' => "100"); mysqli_close ($ link ); // close the connection exit ();} else {exit (json_encode (array ('status' => "success", 'code' => "1 "))); mysqli_close ($ link); // close the connection exit () ;}} else {// insert $ current_day = time () If this user record does not exist (); $ 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); // obtain the dataset if (strpos ($ insertrs, "SELECT") ==false) {// data entry/exit successful upuserscore ('1 '); // exit (json_encode (array ('status' => "success", 'code' => "100") ;}} mysqli_close ($ link ); // close the connection?>

  

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.