PHP session 防止重复登录

zhclass.php

<?php

class zhClass
{

    public $zh;
    // 账号
    public $mm;
    // 密码
    public $con;
    // 数据库句柄
    public function checkZh() // 检测账号是否存在
    {
        $result = mysql_query("SELECT * FROM teacher WHERE id=$this->zh");
        if (mysql_num_rows($result) != 0) {
            return true;
        } else {
            return false;
        }
    }
    public function checkZh2($zh) // 检测账号是否存在
    {
        $result = mysql_query("SELECT * FROM teacher WHERE id=$zh");
        if (mysql_num_rows($result) != 0) {
            return true;
        } else {
            return false;
        }
    }

    public function __construct() // 构造函数 连接到数据库 打开账号数据表
    {
        $this->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

<?php
include 'ZhClass.php';
session_start(); 

$pobj = new zhClass();
if (isset($_SESSION["VVV"])==true) {
    if ($pobj->GetZhSession($_SESSION["VVV"])==session_id())
    {
        echo "已经登录";
    }else {
        echo "登录过期!!!";
    }

    return ;
} else {
    // 未登录
    echo "log";
    return ;
}  

?>

denglu.php

<?php
session_start(); 
include 'ZhClass.php';

$pcode = $_POST['code'];
$obj = json_decode($pcode); 
$pobj = new zhClass();
if($obj->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()<<"尚未登录,正在登录中..";
            this->Loging(); 
        }
        qDebug()<<" POST返回"<<t;
    });


}

bool QmlClass::Loging()
{
    QJsonDocument  doc;
    QJsonObject   obj; 
    obj.insert("id","289672082");
    obj.insert("pwd","12345");
    obj.insert("type",0);
    doc.setObject(obj);
    //设置发送的数据
     QByteArray data;
    data.append(QString("code=%1").arg(doc.toJson().toStdString().c_str()));
    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/denglu.php"));


    //发送请求
    QNetworkReply *reply2=m_netManger->post(network_request, data);
    connect(reply2,&QNetworkReply::readyRead,this,[=](){
        QString t =reply2->readAll();
        qDebug()<<"登录返回值"<<t;
    });

    return   true;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值