浅谈PHP+MYSQL身份验证的方法_PHP
近日在为学校制做校友录时,需要身份验证,在对比之后决定采用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($monthsetType($month,"string");
$month='0'.$month;}
setType($day,"integer");
if($daysetType($day,"string");
$day='0'.$day;}
setType($hour,"integer");
if($hoursetType($min,"string");
$hour='0'.$hour;}
setType($min,"integer");
if($minsetType($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

AI Hentai Generator
Generate AI Hentai for free.

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



With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

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

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

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.

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.

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.

A summary of how to obtain Win11 administrator rights. In the Windows 11 operating system, administrator rights are one of the very important permissions that allow users to perform various operations on the system. Sometimes, we may need to obtain administrator rights to complete some operations, such as installing software, modifying system settings, etc. The following summarizes some methods for obtaining Win11 administrator rights, I hope it can help you. 1. Use shortcut keys. In Windows 11 system, you can quickly open the command prompt through shortcut keys.
