PHP session 防止重复登录的妙招
扫描二维码
随时随地手机看文章
zhclass.php
con = mysql_connect("localhost", "root", "root"); mysql_query("set names 'utf8'"); if (! $this->con) { die('连接服务器失败: ' . 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 "密码错误"; } } 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 "账号不存在!"; return false; } if (! $this->logging($mid, $mm)) { return false; } echo $newName . "," . $mm; // 传过来的是字符串 但是数据库不识别 加上两个单引号转为字符串 '$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 "账号已存在"; return; } if (mysql_query("INSERT INTO teacher (name,id,mima) VALUES ('$nichen',$zh,'$mima')") == false) { echo "注册失败"; } else { echo "注册成功"; } } public function __destruct() // 析构函数销毁数据库连接 { mysql_close($this->con); } } ?>
index.php
GetZhSession($_SESSION["VVV"])==session_id()) { echo "已经登录"; }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登录 ,确定登录后再执行其他post,防止异步获取多个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请求要自己设置请求头 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请求要自己设置请求头 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; //设置头信息 network_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); network_request.setHeader(QNetworkRequest::ContentLengthHeader, data.length()); //设置url network_request.setUrl(QUrl("http://localhost/mycode/Test/index.php")); //发送请求 获取一些关键数据 前提是已近登录 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; }