浅谈PHP+MYSQL身份验证的方法_MySQL
近日在为学校制做校友录时,需要身份验证,在对比之后决定采用PHP+MYSQL进行身份验证。
之前也曾考虑过用cookies或session。但是用cookies,在用户离线再上线后,只要cookies不过期,不用登录仍然可以保持在线,这对于网吧来说是个隐患。而且用户可以关闭cookies,这样身份验证就不成功。也考虑过用session,session在浏览过程中不断的将访问信息加入到session中,如果用户在网站内时间很长,浏览的页面很多,就用导致session越来越大,浏览速度降低,最后只有重新登录,虽然这种情况不多见,但不是我们所希望的。
我在做这个身份验证时的想法是,在身份验证的同时,记录浏览信息。
用户ID在每个页面间传递,ID值是用MD5()函数加密得到的。验证函数是validate_id(),返回值为(0,1),成功为“1”。
思路:
判断被传入的ID值是否为匿名登录ID(a684dd572b1887661782981659331eed),32位,如果是返回0,并且将浏览信息加入数据库。如果否,则查询数据库,看数据库中的用户ID,用户IP与传入的ID,IP值是否相等并且最近浏览时间距当前时间不到20分钟的记录。
判断得到的记录数,如果为0,则认为离线,并用匿名ID登录浏览信息,返回0。记录不为0时,将用户ID,用户IP值,加入数据库,返回1。
创建数据库:
create table logging{
id int unsigned not null primary key auto_increment,
user_id char(32) not null,//用户ID
logging_ip varchar(20) not null,//记录用户IP地址
page_name varchar(30) not null,//浏览网页名
view_time timestamp not null,
student_id varchar(20)
);
创建函数:
/*-----begin function validate-id()---------------
验证用户是否登录
------------------------------------------------*/
function validate_id($link,$id,$ip,$page_name,$student_id=""){
if($id==a684dd572b1887661782981659331eed or $id=''){
$query='insert into logging(user_id,logging_ip,page_name,student_id) values ("a684dd572b1887661782981659331eed","'.$ip.'","'.$page_name.'","anonym");';
$result=mysql_db_query("web",$query,$link);
return (0);
}
else {
$year=strftime("%Y");
$month=strftime("%m");
$day=strftime("%d");
$hour=strftime("%H");
$min=strftime("%M");
$sec=strftime("%S");
echo $time_string=$year.$month.$day.$hour.$min.$sec;
// echo ("
");
// echo "$year-$month-$day $hour-$min-$sec
";
//---------begin if's---------------------------
if(($min-=20) $min+=60;
if(($hour-=1)==-1){
$hour+=24;
if(($day-=1)==0){
switch ($month) {
case 12 :$days=30;break;
case 1 :$days=31;break;
case 2 :if(($year/4==0)and($year/100!=0)or($year/400==0)){
$days=29;}
else {$days=28;}
break;
case 3 :$days=31;break;
case 4 :$days=30;break;
case 5 :$days=31;break;
case 6 :$days=30;break;
case 7 :$days=31;break;
case 8 :$days=31;break;
case 9 :$days=30;break;
case 10 :$days=31;break;
case 11 :$days=30;break;
}
$day+=$days;
if(($month-=1)==0){
$month+=12;
$year-=1;
}
}
}
}
//----------------------------------end if's
setType($month,"integer");
if($month setType($month,"string");
$month='0'.$month;}
setType($day,"integer");
if($day setType($day,"string");
$day='0'.$day;}
setType($hour,"integer");
if($hour setType($min,"string");
$hour='0'.$hour;}
setType($min,"integer");
if($min setType($min,"string");
$min='0'.$min;}
echo '
'.$time_string=$year.$month.$day.$hour.$min.$sec;
// echo "
$year-$month-$day $hour-$min-$sec
";
// echo ("
");
$query="select id from logging where user_id='$id' and logging_ip='$ip' and view_time>'$time_string';";
$result=mysql_db_query("web",$query,$link);
$count=mysql_num_rows($result);
if($count==0){
// echo $query="insert into logging(user_id,logging_ip,page_name) values ('a684dd572b1887661782981659331eed','$ip','$page_name');";
$result=mysql_db_query("web",$query,$link);
return (0);
}
else{
$query="insert into logging(user_id,logging_ip,page_name) values('$id','$ip','$page_name')";
$result=mysql_db_query("web",$query,$link);
return (1);
}
}//end if
}
//----------------------------end function validate-id---------
这个验证方法很简单,而且没有考虑到用户在登录后再登录的情况,大家可以自己加上。
如果用cookies,可能用setcookies()建立用户ID,再从环境变量$HTTP_COOKIE或$HTTP_COOKIE_VARS中读取。都是一样的,不过应该保证用户没有cookies。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





If you find event ID 55, 50, 140 or 98 in the Event Viewer of Windows 11/10, or encounter an error that the disk file system structure is damaged and cannot be used, please follow the guide below to resolve the issue. What does Event 55, File system structure on disk corrupted and unusable mean? At session 55, the file system structure on the Ntfs disk is corrupted and unusable. Please run the chkMSK utility on the volume. When NTFS is unable to write data to the transaction log, an error with event ID 55 is triggered, which will cause NTFS to fail to complete the operation unable to write the transaction data. This error usually occurs when the file system is corrupted, possibly due to the presence of bad sectors on the disk or the file system's inadequacy of the disk subsystem.

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

Colorful motherboards enjoy high popularity and market share in the Chinese domestic market, but some users of Colorful motherboards still don’t know how to enter the bios for settings? In response to this situation, the editor has specially brought you two methods to enter the colorful motherboard bios. Come and try it! Method 1: Use the U disk startup shortcut key to directly enter the U disk installation system. The shortcut key for the Colorful motherboard to start the U disk with one click is ESC or F11. First, use Black Shark Installation Master to create a Black Shark U disk boot disk, and then turn on the computer. When you see the startup screen, continuously press the ESC or F11 key on the keyboard to enter a window for sequential selection of startup items. Move the cursor to the place where "USB" is displayed, and then

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &&, ||, and !. Including using logical AND (&&) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual
