PHP實現登入失敗次數限制

2021-08-08 21:48:21 字數 2604 閱讀 5691

安全對每個**的重要性,不言自明。其中,登陸又是**中比較容易受到攻擊的乙個地方,那麼我們如何對登陸功能的安全性加強呢?

我們先來看一些知名的**是如何做的

user_login_info表

create table `user_login_info` (

`id` int(10) unsigned primary key auto_increment not null,

`uid` int(10) unsigned not null,

`ipaddr` int(10) unsigned not null comment '使用者登陸ip',

`logintime` timestamp not null default current_timestamp on update current_timestamp

comment '使用者登陸時間',

`pass_wrong_time_status` tinyint(10) unsigned not null comment '登陸密碼錯誤狀態'

comment '0 正確 2錯誤'

) engine=innodb default charset=utf8;

user表(使用者表)

create table `user` (

`id` int(10) unsigned not null auto_increment,

`name` varchar(100) not null comment '使用者名稱',

`email` varchar(100) not null,

`pass` varchar(255) not null,

`status` tinyint(3) unsigned not null default '1' comment '1啟用 2禁用',

primary key(id)

) engine=innodb default charset=utf8;

<?php

class login

protected function connectdb()

//顯示登入頁

public function loginpage()

//接受使用者資料做登入

public function handlerlogin()

//檢查使用者最近30分鐘密碼錯誤次數

$res = $this->checkpasswrongtime($userdata['id']);

//錯誤次數超過限制次數

if ( $res === false )

//判斷密碼是否正確

$isrightpass = password_verify($pass, $userdata['pass']);

//登入成功

if ( $isrightpass ) else

}//記錄密碼輸出資訊

protected function recordpasswrongtime($uid)

',2)";

$stmt = $this->pdo->prepare($sql);

$stmt->execute();

}/**

* 檢查使用者最近$min分鐘密碼錯誤次數

* $uid 使用者id

* $min 鎖定時間

* $wtime 錯誤次數

* @return 錯誤次數超過返回false,其他返回錯誤次數,提示使用者

*/protected function checkpasswrongtime($uid, $min=30, $wtime=3)

$time = time();

$prevtime = time() - $min*60;

//使用者所在登入ip

$ip = ip2long( $_server['remote_addr'] );

//pass_wrong_time_status代表使用者輸出了密碼

$sql = "select * from user_login_info where uid= and pass_wrong_time_status=2 and unix_timestamp(logintime) between $prevtime and $time and ipaddr=$ip";

$stmt = $this->pdo->prepare($sql);

$stmt->execute();

$data = $stmt->fetchall(\pdo::fetch_assoc);

//統計錯誤次數

$wrongtime = count($data);

//判斷錯誤次數是否超過限制次數

if ( $wrongtime > $wtime )

return $wrongtime;

}public function __call($methodname, $params)

}$a = @$_get['a']?$_get['a']:'loginpage';

$login = new login();

$login->$a();

限定登入失敗次數,超過指定次數就限制登入一段時間

一。需要的資料庫字段 lastloginerrdate 日期型別 登陸失敗最大次數時的時間 loginerrtimes 整形,預設值為0 登陸失敗的次數 二。前台 三。後台 private int permitlogintimes 5 執行失敗的次數 private int intervalminu...

Oracle取消使用者連續登入失敗次數限制

當使用者連續登入失敗次數過多時,oracle會鎖定該使用者,failed login attempts 用於設定最大次數,超過該值則鎖定該帳號。要取消使用者連續登入失敗次數的限制可以按照以下方法操作 1.輸入以下命令,檢視 failed login attempts 的值 2.輸入以下命令,修改 f...

登入的次數及時間限制

datetime dtone convert.todatetime reader1 errortime tostring datetime dtwo system.datetime.now timespan span dtwo.subtract dtone int g span.minutes 以上...