douzhao7445 2017-04-23 02:13
浏览 138

PHP使用类注销

I need some help on my log out. I try to show less code as possible to avoid long code.

What I'm trying to do is a webpage that allow user to log in and view some stuff. When the user done viewing the stuff, the user are able to log out. When logging out, it'll redirect the user to login page and update my database to clear up all the data such as session_id etc.

But the problem is, whenever the user click the log out button, it'll redirect the user to the login page, but not updating the query which is in the logout function. I'm trying to logs the user out by clearing all the session and data in the database such as session_id, last_log, etc.

Is there any way to make the log out button works?

In my protect class

class protect
{
    var $username = "";
    var $password = "";
    var $id = "";
    var $isAdmin = -1;
    var $sess_id = "";
    var $action = "";
    var $query = "";
    var $ip_address = "";
    var $otp = "";

    function __construct()
    {
         try
         {
            session_start();
            $db = new DB("XXUser","password",DB_NAME);
            $db->connect();

            $this->check_login($db);
            if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin']!=1)
            {
                $this->logout($db);
            }

            else
            {
                if($this->action == "logout")
                {
                   $this->logout($db);
                }

                $this->check_session($db);
            }
         }

         catch
         {
              $this->logout($db);
              exit();
         }
    }

    function post_value()
    {
        if (!empty($_POST))
        {
            foreach ($_POST as $key => $value)
            {
                $this->$key=$value;
            }
        } 
    }

    function get_value()
    {   
        if(isset($_GET['action']))
        {
            $this->action=$_GET['action'];
        }
    }

    function insert_session($db)
    {
        $sql = "UPDATE myuser SET lastLog = now(), active = 'Y', last_active     
        = now(), last_access = now(), ip_addr = '".$this->ip_address."',  
        session_ID = '".trim($this->sess_id)."', fail_login_count = 0, 
        last_fail_login_time ='1900-01-01 00:00:00', otp = 
        '".$_SESSION['otp']."'  WHERE ID = '".$_SESSION['id']."'";

        $db->query($sql);
    }

    function check_session($db)
    {       
        if(isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == 1)
        {
            $sql2 = "SELECT * FROM myuser WHERE ID = '".$_SESSION['id']."'     
            AND otp = '".$_SESSION['otp']."'";

            $db->query($sql2);
            $db->fetchRow();

            if($db->resultCount() == 0)
            {
                echo "<script type=\"text/javascript\">
                    alert(\"Access Denied\");
                </script>";

                session_destroy();
                $db->disconnect();
                header("Location: login2.php");
                exit();

            }

            else
            {
                $this->check_time($db);
                $this->refresh_session();
            }
        }
    }

    function refresh_session()
    {
        //Regenerate id
        session_regenerate_id();

        //Regenerate otp
        $_SESSION['otp'] = trim(md5(time() .$_SESSION['id']));
    }

    function check_time($db)
    {
        $sql3 = "SELECT * FROM myuser WHERE ID = '".$_SESSION['id']."' AND 
        otp = '".$_SESSION['otp']."' AND last_active > DATE_SUB(NOW(), 
        INTERVAL 10 MINUTE)";

        $db->query($sql3);

        if($db->resultCount($db) == 0)
        {
            $this->logout($db);
        }

        else
        {
            $sql2 = "UPDATE myuser SET last_active = now() WHERE ID = 
            '".$_SESSION['id']."' AND otp = '".$_SESSION['otp']."'";

            $db->query($sql2);
        }
    }

    function check_login($db)
    {
        if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin']!=1)
        {
            $this->username = sanitize($_POST['username']);
            $this->password = $_POST['password'];

            $sql = "SELECT * FROM myuser WHERE userName = '".$this-
            >username."' AND userPass = '".$this->password."'";

            $db->query($sql);

            if($db->resultCount() == 0)
            {
                echo "<script type=\"text/javascript\">
                    alert(\"Wrong Username or Password\");
                </script>";

                $db->disconnect();
                $db->clear();
            }

            else
            {
                $db->fetchRow();

                //Correct username but wrong password.
                if($db->record['userName'] == $this->username)
                {
                    if($db->record['userPass'] != $this->password)
                    {
                        echo "<script type=\"text/javascript\">
                            alert(\"Wrong Username or Password\");
                        </script>";
                        $sql3 = "UPDATE myuser SET ip_addr='".$this-
                        >ip_address."',fail_login_count=(fail_login_count+1) 
                        WHERE userName='".$this->username."'";

                        mysql_query($sql3) or die(mysql_error());
                    }

                    else
                    {
                        $this->id = $db->record['ID'];
                        $sql4 = "SELECT * FROM subordinate_reporting WHERE     
                        myuser_uid = '".$this->id."'";

                        $db->query($sql4);

                        if($db->record['active'] == 'Y')
                        {
                            session_destroy();
                            $db->disconnect();
                            header("Location: login2.php");
                            exit();
                        }

                        else if($db->resultCount() == 0)
                        {
                            echo "<script type=\"text/javascript\">
                                alert(\"".$db->record['real_name'].", You     
                                are not authorized to access this page\");
                            </script>";
                            $db->clear();
                        }

                        else
                        {
                            echo "<script type=\"text/javascript\">
                                alert(\"Welcome ".$db-
                                >record['real_name'].". Your last access was     
                                on ".$db->record['last_access']."\");
                            </script>";

                            $this->session($db);
                        }
                    }
                }
            }
        }
    }

    //This function haven't use
    function check_attempt($db)
    {
        $db->query("SELECT fail_login_count, last_fail_login_time FROM 
        myuser WHERE userName = ".$this->username."");
        $db->fetchRow();

        if($db->record['fail_login_count'] >= 3)
        {
            $db->query("UPDATE myuser SET blocked = 'Y', 
            last_fail_login_time = now()");

            echo "<script type=\"text/javascript\">
                alert(\"Your account has been blocked for 10 minutes due to     
                failed login attempts of 3 times\");
            </script>";
        }

        if($db->record['blocked'] === 'Y')
        {
            if(($db->record['last_fail_login_time'] - time()) > 10)
            {
                $db->clear();
                $db->query("UPDATE myuser SET last_fail_login_time = '1900-    
                01-01 00:00:00', fail_login_count = 0, blocked = 'N'");
            }

            else
            {
                $db->clear();
                echo "<script type=\"text/javascript\">
                    alert(\"Please try again later\");
                </script>";
            }
        }
    }

    function logout($db)
    {
            $sql = "UPDATE myuser SET session_ID = '', otp = '', active = 
            'N', last_active = '1900-01-01 00:00:00', lastLog = '1900-01-01         
            00:00:00' WHERE ID = ".$_SESSION['id']." AND 
            otp='".$_SESSION['otp']."'";

            $db->query($sql);
            echo $sql;
            unset ($_SESSION['otp']);
            unset ($_SESSION['loggedin']);
            unset ($_SESSION['id']);
            session_unset();
            session_destroy();
            $db->clear();
            $db->disconnect();
            header("Location: login2.php");
            exit();
    }

    function session($db)
    {
        $_SESSION['loggedin'] = 1;
        $_SESSION['id'] = $this->id;
        $_SESSION['otp'] = trim(md5(time() .$_SESSION['id']));
        $this->ip_address = $this->get_ip();
        $this->sess_id = session_id();
        $_SESSION['timeout'] = time();
        $this->insert_session($db);
    }

    function logout_btn()
    {
            echo "<form name='logoutbtn' method='post' action=''>";
            echo "
&nbsp;<input type='hidden' name='action' value='logout'                 
            />";
            echo "<input type='submit' id='button' value='Log Out' />";
            echo "
</form>";
    }

    function get_ip()
    {
        if(getenv('HTTP_CLIENT_IP'))
        {
            $ip = getenv('HTTP_CLIENT_IP');
        }

        else if(getenv('HTTP_X_FORWARDED_FOR'))
        {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        else
        {                                     
            $ip = getenv('REMOTE_ADDR');
        }

        return $ip;                                         
    }
}

In my normal html file

<?php
try
{
    $prot = new protect();

    if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin']!=1)
    {
        echo "<script type=\"text/javascript\">
            alert("Access Denied");
        </script>";
    }
}

catch (Exception $e)
{
    $e->getMessage();
}
?>

<!DOCTYPE html>
<html>
</html>
<head>
</head>
<body>
$ved = new view_exit_docket($db, $_SESSION['id']);
$ved->check_app_uid($db);
$ved->display_table($db);
$prot->logout_btn();
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doushi9729 2017-04-23 02:49
    关注

    If the log out button was not working, then surely you would not have been redirected - this implies that failures are occurring elsewhere.

    I will assume that some of the stuff you have edited out of your code is critical to its operation (other wise it would not behave as you describe).

    it'll redirect the user to the login page, but not updating the query

    From the code you've shown us, the only route to the redirection is through executing the query. If the data was not changed, then the query failed.

    1) You didn't tell us anything about the DB class.

    2) You don't check the return value from $DB->query() nor poll the state of the operation from $DB after executing the query. If you had, you might have got an error message explaining the problem.

    3) You didn't show us the SQL you are running (the most likely place where the fault lies).

    4) You have not said what happened to the session data

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP