PHP sign-in, with time access

Source: Internet
Author: User

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.
<?php
[Email protected]$_request[' user_id '];
Function: Calculates the difference between two time stamps in days and 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;
}
Count Days
$timediff = $endtime-$starttime;
$days = Intval ($timediff/86400);
Count hours
$remain = $timediff% 86400;
$hours = intval ($remain/3600);
Count the number of minutes
$remain = $remain% 3600;
$mins = Intval ($remain/60);
Calculate 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) {
[Email protected]$_request[' user_id '];
INI file to read 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 Database
Check if there is a user in the integration table, then update the data without inserting 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); Get Data Set
if (! $rs) {
Exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 ', ' Error ' =>mysqli_error ($scorelink)));
}
if (Mysqli_num_rows ($rs)) {//the user has data to update their points information
$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 Data Set
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{//insert their 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); Get Data Set
if (! $rs) {
Exit (Json_encode (' status ' = ' success ', ' Code ' = ' 1 ', ' Error ' =>mysqli_error ($scorelink)));
}
if (Strpos ($q, "select") = = = = False) {
Exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 '));
}
Mysqli_close ($scorelink);//close connection
}
}
INI file to read 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 Database
Check the sign-in table if the user has the update data is not available 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); Get Data Set
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 sign-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 whether the 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 today
Exit (Json_encode (' status ' = ' success ', ' Code ' = ' 5 '));
Mysqli_close ($link);//close connection
Exit ();
}else if ($ary [day]==1) {//No missing sign
$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); Get Data Set
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 the 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 ($link, $usdateq); Get Data Set
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{//the user record is not inserted
$current _day=time ();
$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 succeeded
Upuserscore (' 1 ');
Exit (Json_encode (' status ' = ' success ', ' code ' = ' 100 '));
}
}
Mysqli_close ($link);//close connection
?>

PHP sign-in, with time access

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.