PHP session 防止重復(fù)登錄的妙招
zhclass.php
con?=?mysql_connect("localhost",?"root",?"root"); ????????mysql_query("set?names?'utf8'"); ????????if?(!?$this->con)?{ ????????????die('連接服務(wù)器失敗:?'?.?mysql_error()); ????????}?else?{ ????????????mysql_select_db("mytest",?$this->con); ????????} ????} ????function?logging($id,?$mm)?//?登錄 ????{ ????????$this->zh?=?$id; ????????$this->mm?=?$mm; ????????if?(!?$this->checkZh())?{ ????????????return?false; ????????} ????????//?判斷字段是否存在 ????????$result?=?mysql_query("SELECT?*?FROM?teacher?WHERE?id=$id"); ????????if?(mysql_num_rows($result)?!=?0)?{ ????????????$row?=?mysql_fetch_array($result); ????????????$m_mima?=?$row['mima']; ????????????$name?=?$row['name']; ????????????if?($mm?==?$m_mima)?{ ????????????????echo?"登錄成功"; ????????????????return?true; ????????????}?else?{ ????????????????echo?"密碼錯(cuò)誤"; ????????????} ????????}?else?{ ????????????echo?"登錄失敗"; ????????} ????????return?false; ????} ????function???GetZhSession($zh) ????{ ????????if($this->checkZh2($zh)==false) ????????{ ????????????return??null; ????????} ????????$result?=?mysql_query("SELECT?*?FROM?teacher?WHERE?id=$zh"); ????????if(mysql_num_rows($result)>0) ????????{ ??????????$row?=?mysql_fetch_array($result); ??????????$session?=?$row['sec']; ??????????return??$session; ????????}else?{ ????????????return??null; ????????} ????} ????function???SetZhSession($zh,$sec) ????{ ????????if($this->checkZh2($zh)==false) ????????{ ????????????return??false; ????????} ????????if?(mysql_query("UPDATE?teacher?SET?sec='$sec'?WHERE?id=$zh")==false) ????????{ ????????????return??false; ????????} ????????return??true; ????} ????function?_ChangeUserName($mid,?$mm,?$newName) ????{ ????????$this->zh?=?$mid; ????????$this->mm?=?$mm; ????????if?(!?$this->checkZh())?{ ????????????echo?"賬號(hào)不存在!"; ????????????return?false; ????????} ????????if?(!?$this->logging($mid,?$mm))?{ ????????????return?false; ????????} ????????echo?$newName?.?","?.?$mm; ????????//?傳過來的是字符串?但是數(shù)據(jù)庫不識(shí)別?加上兩個(gè)單引號(hào)轉(zhuǎn)為字符串?'$newName' ????????$ret?=?mysql_query("UPDATE?teacher?SET?name='$newName'?WHERE?id=$mid"); ????????if?(!?$ret)?{ ????????????echo?"?無法修改用戶昵稱?"; ????????} ????} ????public?function?Reg($zh,?$mima,?$nichen) ????{ ????????$this->zh?=?$zh; ????????$this->mm?=?$mima; ????????if?($this->checkZh())?{ ????????????echo?"賬號(hào)已存在"; ????????????return; ????????} ????????if?(mysql_query("INSERT?INTO?teacher?(name,id,mima)?VALUES?('$nichen',$zh,'$mima')")?==?false)?{ ????????????echo?"注冊(cè)失敗"; ????????}?else?{ ????????????echo?"注冊(cè)成功"; ????????} ????} ????public?function?__destruct()?//?析構(gòu)函數(shù)銷毀數(shù)據(jù)庫連接 ????{ ????????mysql_close($this->con); ????} } ?>
index.php
GetZhSession($_SESSION["VVV"])==session_id()) ????{ ????????echo?"已經(jīng)登錄"; ????}else?{ ????????echo?"登錄過期?。?!"; ????} ????return?; }?else?{ ????//?未登錄 ????echo?"log"; ????return?; }?? ?>
denglu.php
type==0){ ????if(?$pobj->logging($obj->id,?$obj->pwd)?==true) ????{ ????????$_SESSION["VVV"]?=?$obj->id; ????????echo?$_SESSION["VVV"]; ????????$pobj->SetZhSession($obj->id,session_id()?); ????????echo?"登陸成功"; ????} }else{ ????echo?"未定義"; } ?>
~~~~~~~~~~~~~~~~~~~~使用xhr post登錄~~~~~~~~~~~~~~~~~~~~~~~~~~~
使用post登錄 ,確定登錄后再執(zhí)行其他post,防止異步獲取多個(gè)session
function??logging() ????{ ????????var??x?=?new?XMLHttpRequest(); ????????x.onreadystatechange?=function() ????????{ ????????????if(x.readyState?==?4)?{ ????????????????if(x.status?==?200)?{ ????????????????????console.log("The?server?replied?with:?"?+?x.responseText); ????????????????????txt.text?=?x.responseText; ????????????????} ????????????} ????????}; ????????var?xxx?=?new?Object; ????????xxx.id="289672082"; ????????xxx.pwd?=?'12345'; ????????xxx.type=0; ????????var?pcode=??JSON.stringify(xxx); ????????x.open("POST","http://192.168.0.105/mycode/Test/denglu.php",true); ????????//post請(qǐng)求要自己設(shè)置請(qǐng)求頭 ????????x.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); ????????x.send("code="+pcode); ????}
如果用戶沒有登錄嘗試其他行為,在返回值中要求用戶登錄:
??function?_PHP_TEST(async) ????{ ????????var??x?=?new?XMLHttpRequest(); ????????x.onreadystatechange?=function() ????????{ ????????????if(x.readyState?==?4)?{ ????????????????if(x.status?==?200)?{ ????????????????????if(x.responseText=="log") ????????????????????{ ????????????????????????console.log("The?server?replied?with:?"?+?x.responseText); ????????????????????????console.log("需要登錄"); ????????????????????????logging(); ????????????????????}else{ ????????????????????????console.log("The?server?replied?with:?"?+?x.responseText); ????????????????????????txt.text?=?x.responseText; ????????????????????} ????????????????} ????????????} ????????}; ????????x.open("POST","http://192.168.0.105/mycode/Test/index.php",async); ????????//post請(qǐng)求要自己設(shè)置請(qǐng)求頭 ????????x.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); ????????x.send(null); ????}
~~~~~~~~~~~~~~~~使用Qt C++登錄~~~~~~~~~~~~~~~~~~~~~~~~~~
QmlClass::QmlClass(QObject?*parent)?:?QObject(parent) { ????QByteArray??data=""; ????m_netManger?=?new?QNetworkAccessManager(this); ????QNetworkRequest?network_request; ????//設(shè)置頭信息 ????network_request.setHeader(QNetworkRequest::ContentTypeHeader,?"application/x-www-form-urlencoded"); ????network_request.setHeader(QNetworkRequest::ContentLengthHeader,?data.length()); ????//設(shè)置url ????network_request.setUrl(QUrl("http://localhost/mycode/Test/index.php")); ????//發(fā)送請(qǐng)求??獲取一些關(guān)鍵數(shù)據(jù)?前提是已近登錄 ????QNetworkReply?*reply2=m_netManger->post(network_request,?data); ????connect(reply2,&QNetworkReply::readyRead,this,[=](){ ????????QString?t?=reply2->readAll(); ????????if(t=="log"??&&?t.length()>0) ????????{ ????????????qDebug()<Loging();? ????????} ????????qDebug()<<"?POST返回"<post(network_request,?data); ????connect(reply2,&QNetworkReply::readyRead,this,[=](){ ????????QString?t?=reply2->readAll(); ????????qDebug()<<"登錄返回值"<<t; ????}); ????return???true; }