近日在为学校制做校友录时,需要身份验证,在对比之后决定采用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。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
